Skip to content

Commit

Permalink
[Feat] Storing users after verification
Browse files Browse the repository at this point in the history
Added users storing if the user has not logged in already.
References #61.
  • Loading branch information
angel-penchev committed Feb 9, 2021
1 parent b5518d0 commit 7f03c0b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
12 changes: 3 additions & 9 deletions server/schema.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
phone_number TEXT NOT NULL,
joined_on TIMESTAMPTZ NOT NULL
id UUID PRIMARY KEY,
token TEXT NOT NULL UNIQUE,
phone_number TEXT NOT NULL UNIQUE
);

-- TO-DO REFERENCES users(id)
Expand Down Expand Up @@ -50,9 +50,3 @@ CREATE TABLE IF NOT EXISTS order_logs (
subject TEXT NOT NULL,
body TEXT NOT NULL
)

CREATE TABLE users(
id INTEGER PRIMARY KEY,
token TEXT NOT NULL,
phone_number TEXT NOT NULL
)
4 changes: 4 additions & 0 deletions server/services/user-service/src/usecases/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import smsApi from '../interfaces/smsApi';
import sharedQueue from '../../../core/interfaces/shared-queue';
import usersDatabase from '../interfaces/users-database';
import {exportToNormalEntity} from '../../../core/entities/utilities';
import buildSendAuthenticationSmsCode from './send-authentication-sms-code';
import buildVerifyAuthenticationSmsCode from './verify-authentication-sms-code';

Expand All @@ -11,6 +13,8 @@ const sendAuthenticationSmsCode = buildSendAuthenticationSmsCode({
const verifyAuthenticationSmsCode = buildVerifyAuthenticationSmsCode({
smsApi,
sharedQueue,
usersDatabase,
exportToNormalEntity,
});

const ordersService = Object.freeze({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import {
SharedQueue,
SMSApi,
SMSVerificationCheckInstance,
User,
UserDatabaseController,
UserExport,
} from '../../../core/@types/global';
import config from '../../../core/config';
import makeUser from '../entities/user';


/**
Expand All @@ -22,16 +26,22 @@ import config from '../../../core/config';
export default function buildVerifyAuthenticationSmsCode({
smsApi,
sharedQueue,
usersDatabase,
exportToNormalEntity,
}: {
smsApi: SMSApi;
sharedQueue: SharedQueue;
usersDatabase: UserDatabaseController;
exportToNormalEntity<T extends Object, U extends Object>(object: T): U;
}): Function {
return async function verifyAuthenticationSmsCode(
phoneNumber: string,
code: string,
) {
// Internal parameter
let verificationInstance: SMSVerificationCheckInstance;
let user: UserExport;
let normalizedUser: User;

// Emitting an 'USER_INVALID_NUMBER' event in shared queue on invalid code
try {
Expand All @@ -47,9 +57,40 @@ export default function buildVerifyAuthenticationSmsCode({
subject: 'USER_INVALID_CODE',
body: {phoneNumber: phoneNumber, error: e.message},
});
return;
return {error: e.message};
}

usersDatabase.findByPhoneNumber(verificationInstance.to).catch((_) => {
try {
// Creating user object
user = makeUser({
phoneNumber: verificationInstance.to,
});
normalizedUser = exportToNormalEntity(user);

// Inserts into database
usersDatabase.insert(normalizedUser).catch((e) => {
throw e;
});

// Notifying logger for a registered user
sharedQueue.emit([
config.inboundLoggerServiceQueue,
], {
subject: 'USER_REGISTERED',
body: {phoneNumber: phoneNumber},
});
} catch (e) {
sharedQueue.emit([
config.inboundLoggerServiceQueue,
], {
subject: 'USER_FAILED_REGISTRATION',
body: {phoneNumber: phoneNumber, error: e.message},
});
return {error: e.message};
}
});

// Emitting an 'USER_LOGGED_IN' event in shared queue on valid code
sharedQueue.emit([
config.inboundDeliveryServiceQueue,
Expand Down

0 comments on commit 7f03c0b

Please sign in to comment.