Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added appsync aurora lambda with tests #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions aws/appsync-aurora-lambda/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ACCOUNT_ID=xxx
DB_PASSWORD=password
DB_PORT=3306
NAME=lambda-aurora-wednesday
RDS_PREFIX=wednesday
RDS_USERNAME=root
DOMAIN=wednesday.is
NAME=appsync-aurora-wednesday
REGION=ap-south-1
ACM_ARN=123
STAGE=dev
42 changes: 42 additions & 0 deletions aws/appsync-aurora-lambda/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require('fs');
const path = require('path');

const prettierOptions = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '.prettierrc'), 'utf8')
);

module.exports = {
parser: 'babel-eslint',
extends: ['prettier-standard'],
plugins: ['prettier'],
env: {
jest: true,
browser: true,
node: true,
es6: true
},
parserOptions: {
ecmaVersion: 6,
sourceType: 'module'
},
rules: {
'prettier/prettier': ['error', prettierOptions],
'arrow-body-style': [2, 'as-needed'],
'class-methods-use-this': 0,
'import/imports-first': 0,
'import/newline-after-import': 0,
'import/no-dynamic-require': 0,
'import/no-extraneous-dependencies': 0,
'import/no-named-as-default': 0,
'import/no-unresolved': 0,
'import/prefer-default-export': 0,
'no-param-reassign': 0,
'max-len': 0,
'newline-per-chained-call': 0,
'no-confusing-arrow': 0,
'no-unused-vars': 2,
'no-use-before-define': 0,
'prefer-template': 2,
'require-yield': 0
}
};
8 changes: 8 additions & 0 deletions aws/appsync-aurora-lambda/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "none"
}
48 changes: 48 additions & 0 deletions aws/appsync-aurora-lambda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<img align="left" src="./appsync_logo.svg" width="480" height="520" />

<div>
<a href="https://www.wednesday.is?utm_source=gthb&utm_medium=repo&utm_campaign=serverless" align="left"><img src="https://uploads-ssl.webflow.com/5ee36ce1473112550f1e1739/5f5879492fafecdb3e5b0e75_wednesday_logo.svg"></a>
<p>
<h1 align="left">Offices and Employees</h1>
</p>
<p>
Write a multi-tenant backend for a SAAS ERP solution which allows storage/retrieval/manipulation of offices and employee data. Employees can work in multiple offices and each office can have multiple employees.
</p>

___


<p>
<h4>
Expert teams of digital product strategists, developers, and designers.
</h4>
</p>

<div>
<a href="https://www.wednesday.is/contact-us?utm_source=gthb&utm_medium=repo&utm_campaign=serverless" target="_blank">
<img src="https://uploads-ssl.webflow.com/5ee36ce1473112550f1e1739/5f6ae88b9005f9ed382fb2a5_button_get_in_touch.svg" width="121" height="34">
</a>
<a href="https://github.com/wednesday-solutions/" target="_blank">
<img src="https://uploads-ssl.webflow.com/5ee36ce1473112550f1e1739/5f6ae88bb1958c3253756c39_button_follow_on_github.svg" width="168" height="34">
</a>
</div>

___

<span>We’re always looking for people who value their work, so come and join us. <a href="https://www.wednesday.is/hiring">We are hiring!</a></span>
</div>




## AppSync

Use AppSync to expose a graphql compliant backend that provides
- a mutation to add/update an office
- a mutation to add/update an employee
- a mutation to associate employees and offices
- a query to fetch all offices
- a query to fetch an office by ID and all its employees
- a query to fetch all employees
- a query to fetch an employee by ID and all the offices the employee works in

22 changes: 22 additions & 0 deletions aws/appsync-aurora-lambda/__mocks__/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import MockDate from 'mockdate';
import moment from 'moment';

MockDate.set('2000-11-22');

export const CONSTANTS = {
uuid: 'uuid',
updatedAt: moment().format('YYYY-MM-DD HH:mm:s:ss'),
employee: {
id: 1,
employeeName: 'Tapan',
officeId: null,
updatedAt: '2020-10-25T16:02:21.000Z'
},
office: {
id: 1,
officeName: 'Wednesday Solutions',
officeAddress: 'Pune, MH, India',
employeeId: null,
updatedAt: '2020-10-25T16:01:55.000Z'
}
};
50 changes: 50 additions & 0 deletions aws/appsync-aurora-lambda/appsync_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions aws/appsync-aurora-lambda/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'entry',
targets: {
node: 'current'
}
}
]
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"arguments": { "employeeId": 1, "officeId": 1 },
"headers": {
"content-type": "application/json",
"x-ws-system-id": "WS"
}
}
27 changes: 27 additions & 0 deletions aws/appsync-aurora-lambda/functions/employee-office/join/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getDB } from '@models';
import { success, failure, logHandler, getSystemId } from '@utils';
export const handler = async (event, context, callback) =>
logHandler(event, async () => {
try {
const { employeeId, officeId } = event.arguments;
if (!getSystemId(event)) {
throw new Error('Request Id Missing!');
}

const { employeeOffice } = getDB();

const res = await employeeOffice.create({
office_id: officeId,
employee_id: employeeId
});

return success(callback, {
id: res.id,
employeeId: res.employee_id,
officeId: res.office_id
});
} catch (err) {
console.log(err);
return failure(callback, err);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "join",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { resetAndMockDB } from '@utils/testUtils';
import { CONSTANTS } from '@mocks/constants';

describe('join-office-employee', () => {
let event;
let mocks;

beforeEach(() => {
event = require('../data.json');
mocks = {
callback: jest.fn()
};
jest.spyOn(mocks, 'callback');
});
it('should update/put an employee', async () => {
await resetAndMockDB(mockDbs => {
mockDbs.employees.findOne = () => CONSTANTS.employee;
});
await resetAndMockDB(mockDbs => {
mockDbs.offices.findOne = () => CONSTANTS.office;
});
await resetAndMockDB(mockDbs => {
mockDbs.employeeOffice.upsert = () => true;
});
const handler = require('../index').handler;
await handler(event, null, mocks.callback);
expect(mocks.callback.mock.calls.length).toBe(1);
expect(mocks.callback.mock.calls[0][0]).toBe(null);
expect(mocks.callback.mock.calls[0][1]).toBeTruthy();
expect(mocks.callback.mock.calls[0][1]).toEqual({ status: true });
});

it('should throw an error when req Id is missing', async () => {
event.headers['x-ws-system-id'] = null;
const handler = require('../index').handler;
await handler(event, null, mocks.callback);
expect(mocks.callback.mock.calls.length).toBe(1);
expect(mocks.callback.mock.calls[0][0]).toStrictEqual(`Request Id Missing!`);
expect(mocks.callback.mock.calls[0][1]).toBe(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"arguments": { "employeeId": 1 },
"headers": {
"content-type": "application/json",
"x-ws-system-id": "WS"
}
}
16 changes: 16 additions & 0 deletions aws/appsync-aurora-lambda/functions/employee/get-employee/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getDB } from '@models';
import { success, failure, logHandler, getSystemId } from '@utils';
export const handler = async (event, context, callback) =>
logHandler(event, async () => {
try {
const { employeeId } = event.arguments;
if (!getSystemId(event)) {
throw new Error('Request Id Missing!');
}
const res = await getDB().employees.findOne({ where: { id: employeeId }, raw: true });
return success(callback, res);
} catch (err) {
console.log(err);
return failure(callback, err);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "getEmployee",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { CONSTANTS } from '@mocks/constants';
import { resetAndMockDB } from '@utils/testUtils';

describe('get-employee', () => {
let event;
let mocks;

beforeEach(() => {
event = require('../data.json');
mocks = {
callback: jest.fn()
};
jest.spyOn(mocks, 'callback');
});
it('should fetch employee with employeeId', async () => {
await resetAndMockDB(mockDbs => {
mockDbs.employees.findOne = () => CONSTANTS.employee;
});
const handler = require('../index').handler;
await handler(event, null, mocks.callback);
expect(mocks.callback.mock.calls.length).toBe(1);
expect(mocks.callback.mock.calls[0][0]).toBe(null);
expect(mocks.callback.mock.calls[0][1]).toBeTruthy();
expect(mocks.callback.mock.calls[0][1]).toEqual(CONSTANTS.employee);
});

it('should throw an error when req Id is missing', async () => {
event.headers['x-ws-system-id'] = null;
const handler = require('../index').handler;
await handler(event, null, mocks.callback);
expect(mocks.callback.mock.calls.length).toBe(1);
expect(mocks.callback.mock.calls[0][0]).toStrictEqual(`Request Id Missing!`);
expect(mocks.callback.mock.calls[0][1]).toBe(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"arguments": { "officeId": 1 },
"headers": {
"content-type": "application/json",
"x-ws-system-id": "WS"
}
}
Loading