Skip to content

Commit

Permalink
feat(emails): add endpoint for random email using user first name and…
Browse files Browse the repository at this point in the history
… last name

Closes #81
  • Loading branch information
sergejomon committed Oct 24, 2024
1 parent e08e9f9 commit 982e74f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
11 changes: 9 additions & 2 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 44 additions & 1 deletion api/src/modules/emails/api/emails-routes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Request, Response } from 'express';
import * as core from 'express-serve-static-core';
import { getRandomEmails } from '../utils/getRandomEmails';
import {getRandomEmailByUsername} from "../utils/getRandomEmailByUsername";

const defaultEmailCount = 10;

module.exports = function (app: core.Express) {

Check warning on line 8 in api/src/modules/emails/api/emails-routes.ts

View check run for this annotation

Codeac.io / Codeac Code Quality

only-arrow-functions

non-arrow functions are forbidden
/**
* @openapi
* '/email':
* '/emails':
* get:
* tags:
* - Emails
Expand All @@ -21,4 +22,46 @@ module.exports = function (app: core.Express) {
app.get('/emails', (req: Request, res: Response) => {
res.json(getRandomEmails(defaultEmailCount));
});

/**
* @openapi
* '/email':
* get:
* tags:
* - Email
* summary: Returns a movk of email using first and last name
* parameters:
* - in: query
* name: firstName
* required: true
* schema:
* type: string
* description: The first name of the user
* - in: query
* name: lastName
* required: true
* schema:
* type: string
* description: The last name of the user
* responses:
* '200':
* description: OK
* content:
* application/json:
* schema:
* $ref: '#/definitions/MockEmails'
* '400':
* description: Bad Request
* content:
* application/json:
*/

app.get('/emails/address', (req: Request, res: Response) => {
const firstName = req.query.firstName as string;
const lastName = req.query.lastName as string;
if (!firstName || !lastName) {
return res.status(400).json({ error: 'firstName and lastName are required' });
}
res.json(getRandomEmailByUsername( firstName.toLowerCase(), lastName.toLowerCase()));
});
};
31 changes: 31 additions & 0 deletions api/src/modules/emails/tests/api/emails-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,35 @@ describe('emails api endpoints', () => {
expect(response.body[0]).toHaveProperty('date');
});
});

describe('GET /emails/address', () => {
it('should return a random email', async () => {
const response = await request(app).get(`/emails/address`)
.query({firstName: 'John', lastName: 'Cena'})
.expect(200).expect('Content-Type', /json/);

expect(response.body).toContain('john');
expect(response.body).toMatch(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);
});
it('should return 400 error', async () => {
const response = await request(app).get(`/emails/address`)
.query({firstName: 'John'})
.expect(400).expect('Content-Type', /json/);

expect(response.body).toEqual({"error": "firstName and lastName are required"});
});
it('should return 400 error', async () => {
const response = await request(app).get(`/emails/address`)
.query({lastName: 'Cena'})
.expect(400).expect('Content-Type', /json/);

Check warning on line 38 in api/src/modules/emails/tests/api/emails-routes.test.ts

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 8 lines is too similar to api/src/modules/emails/tests/api/emails-routes.test.ts:37

expect(response.body).toEqual({"error": "firstName and lastName are required"});
});
it('should return 400 error', async () => {
const response = await request(app).get(`/emails/address`)
.expect(400).expect('Content-Type', /json/);

Check warning on line 45 in api/src/modules/emails/tests/api/emails-routes.test.ts

View check run for this annotation

Codeac.io / Codeac Code Quality

CodeDuplication

This block of 8 lines is too similar to api/src/modules/emails/tests/api/emails-routes.test.ts:30
expect(response.body).toEqual({"error": "firstName and lastName are required"});
});
});
});
6 changes: 6 additions & 0 deletions api/src/modules/emails/utils/getRandomEmailByUsername.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { faker } from '@faker-js/faker';
import Email from '../consts/Email';

export const getRandomEmailByUsername = (firstName: string, lastName:string): string => {
return faker.internet.email(firstName,lastName);
};

0 comments on commit 982e74f

Please sign in to comment.