Skip to content

Commit

Permalink
[Feat] Users database interface
Browse files Browse the repository at this point in the history
Added a interface for interacting with the database.
References #61.
  • Loading branch information
angel-penchev committed Feb 9, 2021
1 parent ee83801 commit 37dd634
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 4 deletions.
20 changes: 20 additions & 0 deletions server/services/core/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ export interface UserExport {
getPhoneNumber(): string;
}

/* eslint-disable camelcase */
export interface UserDatabaseSchema {
id: string;
token: string;
phone_number: string;
}
/* eslint-enable camelcase */

/**
* HTTP Request object structure.
*
Expand Down Expand Up @@ -587,6 +595,18 @@ export interface OrderLogsDatabaseController {
}: QueueMessage<any>): Promise<{ id: string } | { error: string; }>;
}

export interface UserDatabaseController {
insert({
id,
token,
phoneNumber,
}: User): Promise<{ id: string } | { error: string; }>;

findById(
userId: string,
): Promise<User | { error: string; }>;
}

/**
* Database Query Results object structure.
*
Expand Down
8 changes: 4 additions & 4 deletions server/services/user-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import makeExpressCallback from '../../core/express-callback';
import config from '../../core/config';
import {
postAuthenticateSendSms,
postAuthenticateVerifyToken,
postAuthenticateVerifyCode,
notFound,
} from './interfaces/controllers';

Expand All @@ -13,12 +13,12 @@ const app = express();
app.use(bodyParser.json());

app.post(
`${apiRoot}/users/authenticate/step1`,
`${apiRoot}/users/authenticate/send-sms-code`,
makeExpressCallback(postAuthenticateSendSms),
);
app.post(
`${apiRoot}/users/authenticate/step1`,
makeExpressCallback(postAuthenticateVerifyToken),
`${apiRoot}/users/authenticate/verify-sms-code`,
makeExpressCallback(postAuthenticateVerifyCode),
);
app.use(makeExpressCallback(notFound));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {Pool as PostgresPool} from 'pg';
import makeOrdersDatabase from './users-database';
import config from '../../../../core/config';

const databasePool = new PostgresPool({
host: config.postgresUrl,
port: config.postgresPort,
user: config.postgresUser,
password: config.postgresPassword,
database: config.postgresDb,
});

const ordersDatabase = makeOrdersDatabase({
databaseClient: databasePool,
databaseTable: 'users',
});

export default ordersDatabase;
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
DatabaseClient,
DatabaseQueryResults,
UserDatabaseController,
UserDatabaseSchema,
User,
} from '../../../../core/@types/global';

/**
* Database interactions interface.
*
* @export
* @param {({
* databaseClient: DatabaseClient,
* databaseTable: string
* })} {
* databaseClient,
* databaseTable,
* } - dependency injection
* @return {UserDatabaseController} - database controller object
*/
export default function makeUsersDatabase({
databaseClient,
databaseTable,
}: {
databaseClient: DatabaseClient,
databaseTable: string
}): UserDatabaseController {
/**
* Finds an entry in the database.
*
* @param {string} userId
* @return {
* Promise<User | { error: string; }>
* } - user entry from database
*/
async function findById(
userId: string,
): Promise<User | { error: string; }> {
const resultRows: void | DatabaseQueryResults<UserDatabaseSchema> = await
databaseClient.query(`
SELECT * FROM ${databaseTable} WHERE id = $1
`, [userId],
).catch((e: Error) => console.log(e));

if (!resultRows) {
return {error: 'No such user found.'};
}

const result = resultRows.rows[0];
return Object.freeze({
id: result.id,
token: result.token,
phoneNumber: result.phone_number,
});
}

/**
* Inserts an entry in the database.
*
* @param {User} {
* id,
* sender,
* receiver,
* source,
* createdOn,
* } - user details
* @return {string} - user id
*/
async function insert({
id,
token,
phoneNumber,
}: User): Promise<{ id: string } | { error: string; }> {
const resultRows: void | DatabaseQueryResults<UserDatabaseSchema> = await
databaseClient.query(`
INSERT INTO ${databaseTable}
(
id,
token,
phone_number,
)
VALUES ($1, $2, $3)
RETURNING id
`, [id, token, phoneNumber],
).catch((e: Error) => console.log(e));

if (!resultRows) {
return {error: 'Error creating user.'};
}

const result = resultRows.rows[0];
return {id: result.id};
}

return Object.freeze({
findById: findById,
insert: insert,
});
}

0 comments on commit 37dd634

Please sign in to comment.