Skip to content

Commit

Permalink
[Feat] Users controllers
Browse files Browse the repository at this point in the history
Added controllers for handling sms sending and code verification.
References #61.
  • Loading branch information
angel-penchev committed Feb 8, 2021
1 parent b528447 commit 75c6e59
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
29 changes: 29 additions & 0 deletions server/services/user-service/src/interfaces/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import notFound from '../../../../core/interfaces/controllers/not-found';
import buildPostAuthenticateSendSms from './post-authenticate-send-sms-token';
import buildPostAuthenticateVerifyToken
from './post-authenticate-verify-sms-token';
import {
sendAuthenticationSmsCode,
verifyAuthenticationSmsCode,
} from '../../usecases';

const postAuthenticateSendSms = buildPostAuthenticateSendSms({
sendAuthenticationSmsCode,
});

const postAuthenticateVerifyToken = buildPostAuthenticateVerifyToken({
verifyAuthenticationSmsCode,
});

const orderController = Object.freeze({
postAuthenticateSendSms,
postAuthenticateVerifyToken,
notFound,
});

export default orderController;
export {
postAuthenticateSendSms,
postAuthenticateVerifyToken,
notFound,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {HttpRequest} from '../../../../core/@types/global';

/**
* Express controller for handling of POST "users/authenticate/send-sms-token".
*
* @export
* @param {{sendAuthenticationSmsCode: Function}} {
* sendAuthenticationSmsCode
* } - dependency injection
* @return {Function} - post users send sms token controller builder function
*/
export default function buildPostAuthenticateSendSms({
sendAuthenticationSmsCode,
}: {sendAuthenticationSmsCode: Function}): Function {
return async function postAuthenticateSendSms(httpRequest: HttpRequest) {
try {
const phoneNumber = httpRequest.body['phoneNumber'];
const verificationInstance = await sendAuthenticationSmsCode(
phoneNumber,
);
return {
headers: {
'Content-Type': 'application/json',
},
statusCode: 201,
body: {verificationInstance},
};
} catch (e) {
return {
headers: {
'Content-Type': 'application/json',
},
statusCode: 400,
body: {
error: e.message,
},
};
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {HttpRequest} from '../../../../core/@types/global';

/**
* Express controller for handling of POST "users/authenticate/verify-sms-token"
*
* @export
* @param {{verifyAuthenticationSmsCode: Function}} {
* verifyAuthenticationSmsCode
* } - dependency injection
* @return {Function} - post users send sms token controller builder function
*/
export default function buildPostAuthenticateVerifyToken({
verifyAuthenticationSmsCode,
}: {verifyAuthenticationSmsCode: Function}): Function {
return async function postAuthenticateVerifyToken(httpRequest: HttpRequest) {
try {
const verificationInfo = httpRequest.body;
const verificationInstance = await verifyAuthenticationSmsCode(
verificationInfo,
);
return {
headers: {
'Content-Type': 'application/json',
},
statusCode: 201,
body: {verificationInstance},
};
} catch (e) {
return {
headers: {
'Content-Type': 'application/json',
},
statusCode: 400,
body: {
error: e.message,
},
};
}
};
}

0 comments on commit 75c6e59

Please sign in to comment.