-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
427 additions
and
73 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
backend/typescript/middlewares/validators/teamMemberValidators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { validatePrimitive, getApiValidationError } from "./util"; | ||
|
||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
/* eslint-disable-next-line import/prefer-default-export */ | ||
export const createTeamMemberDtoValidator = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
if (!validatePrimitive(req.body.firstName, "string")) { | ||
return res.status(400).send(getApiValidationError("firstName", "string")); | ||
} | ||
if (!validatePrimitive(req.body.lastName, "string")) { | ||
return res.status(400).send(getApiValidationError("lastName", "string")); | ||
} | ||
if (!validatePrimitive(req.body.teamRole, "string")) { | ||
return res.status(400).send(getApiValidationError("role", "string")); | ||
} | ||
return next(); | ||
}; |
34 changes: 34 additions & 0 deletions
34
...nd/typescript/migrations/2024.10.01T20.44.32.2024.10.01T4.43.30.add-team-members-table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { DataType } from "sequelize-typescript"; | ||
|
||
import { Migration } from "../umzug"; | ||
import { teamRoleEnum } from "../types"; | ||
|
||
const TABLE_NAME = "team_members"; | ||
|
||
export const up: Migration = async ({ context: sequelize }) => { | ||
await sequelize.getQueryInterface().createTable(TABLE_NAME, { | ||
id: { | ||
type: DataType.INTEGER, | ||
allowNull: false, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
first_name: { | ||
type: DataType.STRING, | ||
allowNull: false, | ||
}, | ||
last_name: { | ||
type: DataType.STRING, | ||
allowNull: false, | ||
}, | ||
team_role: { | ||
type: DataType.ENUM, | ||
values: teamRoleEnum, | ||
allowNull: false, | ||
}, | ||
}); | ||
}; | ||
|
||
export const down: Migration = async ({ context: sequelize }) => { | ||
await sequelize.getQueryInterface().dropTable(TABLE_NAME); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { | ||
Column, | ||
DataType, | ||
Model, | ||
Table, | ||
AllowNull, | ||
} from "sequelize-typescript"; | ||
import { TeamRole, teamRoleEnum } from "../types"; | ||
|
||
@Table({ timestamps: false, tableName: "team_members" }) | ||
export default class TeamMember extends Model { | ||
@Column({ type: DataType.STRING, allowNull: false }) | ||
first_name!: string; | ||
|
||
@Column({ type: DataType.STRING, allowNull: false }) | ||
last_name!: string; | ||
|
||
@AllowNull(false) | ||
@Column({ | ||
type: DataType.ENUM(...Object.values(teamRoleEnum)), | ||
allowNull: false, | ||
}) | ||
team_role!: TeamRole; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Router } from "express"; | ||
import ITeamMemberService from "../services/interfaces/teamMemberService"; | ||
import TeamMemberService from "../services/implementations/teamMemberService"; | ||
import { CreateTeamMemberDTO, TeamMemberDTO, UserDTO } from "../types"; | ||
import { getErrorMessage } from "../utilities/errorUtils"; | ||
import { createTeamMemberDtoValidator } from "../middlewares/validators/teamMemberValidators"; | ||
|
||
const teamMemberRouter: Router = Router(); | ||
|
||
const teamMemberService: ITeamMemberService = new TeamMemberService(); | ||
|
||
/* Get all team members */ | ||
teamMemberRouter.get("/", async (req, res) => { | ||
try { | ||
const teamMembers = await teamMemberService.getTeamMembers(); | ||
res.status(200).json(teamMembers); | ||
} catch (error: unknown) { | ||
res.status(500).json({ error: getErrorMessage(error) }); | ||
} | ||
}); | ||
|
||
teamMemberRouter.post("/", createTeamMemberDtoValidator, async (req, res) => { | ||
const data: CreateTeamMemberDTO = req.body; | ||
try { | ||
const newTeamMember = await teamMemberService.createTeamMember(data); | ||
res.status(201).json(newTeamMember); | ||
} catch (error: unknown) { | ||
res.status(500).json({ error: getErrorMessage(error) }); | ||
} | ||
}); | ||
|
||
export default teamMemberRouter; |
54 changes: 54 additions & 0 deletions
54
backend/typescript/services/implementations/teamMemberService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import PgTeamMember from "../../models/teamMember.model"; | ||
import { CreateTeamMemberDTO, TeamMemberDTO } from "../../types"; | ||
import { getErrorMessage, NotFoundError } from "../../utilities/errorUtils"; | ||
import logger from "../../utilities/logger"; | ||
import ITeamMemberService from "../interfaces/teamMemberService"; | ||
|
||
const Logger = logger(__filename); | ||
|
||
class TeamMemberService implements ITeamMemberService { | ||
/* eslint-disable class-methods-use-this */ | ||
|
||
async getTeamMembers(): Promise<TeamMemberDTO[]> { | ||
try { | ||
const teamMembers: Array<PgTeamMember> = await PgTeamMember.findAll(); | ||
return teamMembers.map((teamMember) => ({ | ||
id: String(teamMember.id), | ||
firstName: teamMember.first_name, | ||
lastName: teamMember.last_name, | ||
teamRole: teamMember.team_role, | ||
})); | ||
} catch (error: unknown) { | ||
Logger.error( | ||
`Failed to get team members. Reason = ${getErrorMessage(error)}`, | ||
); | ||
throw error; | ||
} | ||
} | ||
|
||
async createTeamMember( | ||
teamMember: CreateTeamMemberDTO, | ||
): Promise<TeamMemberDTO> { | ||
let newTeamMember: PgTeamMember | null; | ||
try { | ||
newTeamMember = await PgTeamMember.create({ | ||
first_name: teamMember.firstName, | ||
last_name: teamMember.lastName, | ||
team_role: teamMember.teamRole, | ||
}); | ||
} catch (error: unknown) { | ||
Logger.error( | ||
`Failed to create team member. Reason = ${getErrorMessage(error)}`, | ||
); | ||
throw error; | ||
} | ||
return { | ||
id: String(newTeamMember.id), | ||
firstName: newTeamMember.first_name, | ||
lastName: newTeamMember.last_name, | ||
teamRole: newTeamMember.team_role, | ||
}; | ||
} | ||
} | ||
|
||
export default TeamMemberService; |
20 changes: 20 additions & 0 deletions
20
backend/typescript/services/interfaces/teamMemberService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { CreateTeamMemberDTO, TeamMemberDTO } from "../../types"; | ||
|
||
interface ITeamMemberService { | ||
/** | ||
* Get a list of all the team members in the database | ||
* @returns a TeamMemberDTO with a team member's information | ||
* @throws Error if team member retrieval fails | ||
*/ | ||
getTeamMembers(): Promise<TeamMemberDTO[]>; | ||
|
||
/** | ||
* Create a team member | ||
* @param teamMember the team member to be created | ||
* @returns a TeamMemberDTO with the created team member's information | ||
* @throws Error if team member creation fails | ||
*/ | ||
createTeamMember(teamMember: CreateTeamMemberDTO): Promise<TeamMemberDTO>; | ||
} | ||
|
||
export default ITeamMemberService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { TeamMember, TeamRole } from "../types/TeamMemberTypes"; | ||
import baseAPIClient from "./BaseAPIClient"; | ||
|
||
const get = async (): Promise<TeamMember[]> => { | ||
try { | ||
const { data } = await baseAPIClient.get("team-members/"); | ||
return data; | ||
} catch (error) { | ||
throw new Error(`Failed to get team members: ${error}`); | ||
} | ||
}; | ||
|
||
const create = async ( | ||
firstName: string, | ||
lastName: string, | ||
teamRole: TeamRole, | ||
): Promise<TeamMember[] | null> => { | ||
try { | ||
const { data } = await baseAPIClient.post("team-members/", { | ||
firstName, | ||
lastName, | ||
teamRole, | ||
}); | ||
return data; | ||
} catch (error: unknown) { | ||
return null; | ||
} | ||
}; | ||
|
||
export default { get, create }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.