Skip to content

Instantly share code, notes, and snippets.

@the-frey
Created June 15, 2020 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-frey/f6a03711f3f811056166d0626c577491 to your computer and use it in GitHub Desktop.
Save the-frey/f6a03711f3f811056166d0626c577491 to your computer and use it in GitHub Desktop.
// -- handler namespace --
'use strict';
import * as _ from 'lodash';
import * as responses from '../../common/responses';
import * as db from '../../common/db';
import * as uDb from '../../common/users/db';
import * as uXfms from '../../common/users/transformations';
export const usersGet = async (event): Promise<any> => {
const client = await db.getDbConn();
try {
const userId = event.pathParameters.id;
const getUserRes = await uDb.getUserById(client, [userId]);
const user = getUserRes.rows[0];
if (_.isNil(user)) {
await db.closeDbConn(client);
return responses.respond404('project-name-here/error', 'User does not exist');
}
await db.closeDbConn(client);
const userResponse = uXfms.dbUserToCamelCase(user);
return responses.respond200('project-name-here/user', userResponse);
} catch (err) {
await db.closeDbConn(client);
console.log(err);
return responses.respond500('project-name-here/error', 'Something went wrong');
}
};
// -- responses.ts --
interface Headers {
'Access-Control-Allow-Origin': string;
'Access-Control-Allow-Credentials': boolean;
}
interface Response {
headers: Headers;
statusCode: number;
body: string; // JSON stringified body
}
const variantResponse = (type, payload): string => {
return JSON.stringify([type, payload]);
};
const respond200 = (type, payload, headers = {}): Response => {
return {
headers: _.merge(corsHeaders(), headers), // cors headers have some defaults and env
statusCode: 200,
body: variantResponse(type, payload)
};
};
// -- handler tests namespace --
import * as db from '../../../src/common/db';
import * as uDb from '../../../src/common/users/db';
import * as handlers from '../../../src/handlers/users/get';
const truncateTablesQuery = `<truncation db query here>`;
afterEach(async () => {
const client = await db.getDbConn();
await client.query(truncateTablesQuery);
await db.closeDbConn(client);
});
test('get handler returns an existing stub user', async () => {
const client = await db.getDbConn();
const userValues = [1, 'jeff.vader@example.com'];
await uDb.insertStubUser(client, userValues);
await db.closeDbConn(client);
const expected = {
'id': '1',
'fullName': null,
'phoneNumber': null,
'email': 'jeff.vader@example.com',
};
const httpEvent = {
pathParameters: {
id: 1
}
};
const response = await handlers.usersGet(httpEvent);
const code = response.statusCode;
expect(code).toEqual(200);
const body = JSON.parse(response.body);
const actual = body[1];
expect(actual).toEqual(expected);
});
test('get handler returns 404 if user does not exist', async () => {
const expected = 'User does not exist';
const httpEvent = {
pathParameters: {
id: 1
}
};
const response = await handlers.usersGet(httpEvent);
const code = response.statusCode;
expect(code).toEqual(404);
const body = JSON.parse(response.body);
const actual = body[1];
expect(actual).toEqual(expected);
});
// -- transformations test namespace --
// this is basically what we get back from a DB select
const exampleUser = {
id: 1,
full_name: null,
phone_number: null,
email: 'jeff.vader@example.com'
};
test('dbUserToCamelCase converts a snake_case db representation to camelCase', () => {
const expected = {
id: 1,
fullName: null,
phoneNumber: null,
email: 'jeff.vader@example.com'
};
const actual = uXfms.dbUserToCamelCase(exampleUser);
expect(actual).toEqual(expected);
});
// -- transformations namespace --
// this isn't quite the reality, but it gives you the idea
interface CamelCaseUser {
id: number;
fullName: string;
phoneNumber: string;
email: string;
}
// convert the db representation to the json version
const dbUserToCamelCase = (dbRowObj): CamelCaseUser => {
const {
id,
full_name,
phone_number,
email
} = dbRowObj;
const camelCaseVersion = {
id: id,
fullName: full_name,
phoneNumber: phone_number,
email: email
};
return camelCaseVersion;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment