@@ -21,47 +21,46 @@ const create = async function ({ email, temporaryKey }) {
21
21
22
22
/**
23
23
* @param {string } email
24
- */
25
- const markAsBeingUsed = async function ( email ) {
26
- const knexConn = DomainTransaction . getConnection ( ) ;
27
-
28
- await knexConn ( RESET_PASSWORD_DEMANDS_TABLE_NAME )
29
- . whereRaw ( 'LOWER("email") = LOWER(?)' , email )
30
- . update ( { used : true , updatedAt : new Date ( ) } ) ;
31
- } ;
32
-
33
- /**
34
24
* @param {string } temporaryKey
35
25
*
36
- * @returns {ResetPasswordDemand } retrieved reset password demand
26
+ * @returns {Promise<void> }
27
+ * @throws PasswordResetDemandNotFoundError when resetPasswordDemand has been already used or does not exist
37
28
*/
38
- const findByTemporaryKey = async function ( temporaryKey ) {
29
+ const markAsUsed = async function ( email , temporaryKey ) {
39
30
const knexConn = DomainTransaction . getConnection ( ) ;
40
31
41
32
const resetPasswordDemand = await knexConn ( RESET_PASSWORD_DEMANDS_TABLE_NAME )
42
- . select ( '*' )
33
+ . whereRaw ( 'LOWER("email") = LOWER(?)' , email )
43
34
. where ( { temporaryKey, used : false } )
44
- . first ( ) ;
35
+ . update ( { used : true , updatedAt : new Date ( ) } ) ;
45
36
46
37
if ( ! resetPasswordDemand ) {
47
38
throw new PasswordResetDemandNotFoundError ( ) ;
48
39
}
49
-
50
- return _toDomain ( resetPasswordDemand ) ;
51
40
} ;
52
41
53
42
/**
54
43
* @param {string } email
44
+ */
45
+ const markAllAsUsedByEmail = async function ( email ) {
46
+ const knexConn = DomainTransaction . getConnection ( ) ;
47
+
48
+ await knexConn ( RESET_PASSWORD_DEMANDS_TABLE_NAME )
49
+ . whereRaw ( 'LOWER("email") = LOWER(?)' , email )
50
+ . where ( { used : false } )
51
+ . update ( { used : true , updatedAt : new Date ( ) } ) ;
52
+ } ;
53
+
54
+ /**
55
55
* @param {string } temporaryKey
56
56
*
57
57
* @returns {ResetPasswordDemand } retrieved reset password demand
58
58
*/
59
- const findByUserEmail = async function ( email , temporaryKey ) {
59
+ const findByTemporaryKey = async function ( temporaryKey ) {
60
60
const knexConn = DomainTransaction . getConnection ( ) ;
61
61
62
62
const resetPasswordDemand = await knexConn ( RESET_PASSWORD_DEMANDS_TABLE_NAME )
63
63
. select ( '*' )
64
- . whereRaw ( 'LOWER("email") = LOWER(?)' , email )
65
64
. where ( { temporaryKey, used : false } )
66
65
. first ( ) ;
67
66
@@ -83,18 +82,19 @@ const removeAllByEmail = async function (email) {
83
82
84
83
/**
85
84
* @typedef {Object } ResetPasswordDemandRepository
85
+ * @property {function } markAsUsed
86
86
* @property {function } create
87
87
* @property {function } deleteByUserEmail
88
88
* @property {function } findByTemporaryKey
89
- * @property {function } findByUserEmail
90
- * @property {function } markAsBeingUsed
89
+ * @property {function } getByUserEmail
90
+ * @property {function } markAllAsUsedByEmail
91
91
*/
92
92
const resetPasswordDemandRepository = {
93
+ markAsUsed,
93
94
create,
94
95
removeAllByEmail,
95
96
findByTemporaryKey,
96
- findByUserEmail,
97
- markAsBeingUsed,
97
+ markAllAsUsedByEmail,
98
98
} ;
99
99
100
100
export { resetPasswordDemandRepository } ;
0 commit comments