-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a usecase for sending verification SMS. References #61.
- Loading branch information
1 parent
2a174d0
commit d556200
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
server/services/user-service/src/usecases/send-authentication-sms-code.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
SharedQueue, | ||
SMSApi, | ||
SMSVerificationInstance, | ||
} from '../../../core/@types/global'; | ||
import config from '../../../core/config'; | ||
|
||
|
||
/** | ||
* Sends a authentication SMS code. | ||
* | ||
* @export | ||
* @param {{ | ||
* smsApi: SMSApi, | ||
* sharedQueue: SharedQueue, | ||
* }} { | ||
* smsApi, | ||
* sharedQueue, | ||
* } - dependency injection | ||
* @return {Function} | ||
*/ | ||
export default function buildSendAuthenticationSmsCode({ | ||
smsApi, | ||
sharedQueue, | ||
}: { | ||
smsApi: SMSApi; | ||
sharedQueue: SharedQueue; | ||
}): Function { | ||
return async function sendAuthenticationSmsCode(phoneNumber: string) { | ||
// Internal parameter | ||
let verificationInstance: SMSVerificationInstance; | ||
|
||
// Emitting an 'USER_INVALID_NUMBER' event in shared queue on invalid number | ||
try { | ||
// Sending verification code | ||
verificationInstance = await smsApi.sendCode( | ||
phoneNumber, | ||
'sms', | ||
); | ||
} catch (e) { | ||
sharedQueue.emit([ | ||
config.inboundLoggerServiceQueue, | ||
], { | ||
subject: 'USER_INVALID_NUMBER', | ||
body: {phoneNumber: phoneNumber, error: e.message}, | ||
}); | ||
return; | ||
} | ||
|
||
// Emitting an 'USER_SMS_SEND' event in shared queue on valid number | ||
sharedQueue.emit([ | ||
config.inboundDeliveryServiceQueue, | ||
config.inboundLoggerServiceQueue, | ||
], { | ||
subject: 'USER_SMS_SEND', | ||
body: {phoneNumber: phoneNumber}, | ||
}); | ||
|
||
return verificationInstance; | ||
}; | ||
} |