Skip to content
Merged
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
22 changes: 22 additions & 0 deletions src/backend/src/controllers/recruitment.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,28 @@ export default class RecruitmentController {
}
}

static async editGuestDefinition(req: Request, res: Response, next: NextFunction) {
try {
const { definitionId } = req.params as Record<string, string>;
const { term, description, order, icon, buttonText, buttonLink } = req.body;

const definition = await RecruitmentServices.editGuestDefinition(
req.currentUser,
req.organization,
term,
description,
definitionId,
order,
icon,
buttonText,
buttonLink
);
res.status(200).json(definition);
} catch (error: unknown) {
next(error);
}
}

static async deleteGuestDefinition(req: Request, res: Response, next: NextFunction) {
try {
const { definitionId } = req.params as Record<string, string>;
Expand Down
12 changes: 12 additions & 0 deletions src/backend/src/routes/recruitment.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ recruitmentRouter.post(
RecruitmentController.createGuestDefinition
);

recruitmentRouter.post(
'/guestDefinition/:guestId/edit',
nonEmptyString(body('term')),
nonEmptyString(body('description')),
body('order').isInt(),
nonEmptyString(body('icon')).optional(),
nonEmptyString(body('buttonText')).optional(),
nonEmptyString(body('buttonLink')).optional(),
validateInputs,
RecruitmentController.editGuestDefinition
);

recruitmentRouter.delete('/guestdefinition/:definitionId/delete', RecruitmentController.deleteGuestDefinition);
recruitmentRouter.get('/guestdefinitions', RecruitmentController.getAllGuestDefintions);

Expand Down
56 changes: 56 additions & 0 deletions src/backend/src/services/recruitment.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,60 @@ export default class RecruitmentServices {
data: { dateDeleted: new Date(), userDeletedId: deleter.userId }
});
}

/**
* Edits guest definition
* @param creator user editing the definition
* @param organization org the definition is being edited
* @param term the term we are editing
* @param description the definition of the term
* @param order the order the term appears on the page
* @param icon the icon associated with the term
* @param buttonText the text displayed on the terms button
* @param buttonLink where the terms button links to
* @returns
*/
static async editGuestDefinition(
creator: User,
organization: Organization,
term: string,
description: string,
definitionId: string,
order: number,
icon?: string,
buttonText?: string,
buttonLink?: string
) {
if (!(await userHasPermission(creator.userId, organization.organizationId, isAdmin)))
throw new AccessDeniedAdminOnlyException('edit a guest definition');

const currentGuestDefinition = await prisma.guest_Definition.findUnique({
where: {
definitionId
}
});

if (!currentGuestDefinition) {
throw new NotFoundException('Guest Definition', definitionId);
}

if (currentGuestDefinition.dateDeleted) {
throw new DeletedException('Guest Definition', definitionId);
}

const updatedGuest = await prisma.guest_Definition.update({
where: {
definitionId
},
data: {
term,
description,
order,
icon,
buttonText,
buttonLink
}
});
return guestDefinitionTransformer(updatedGuest);
}
}
124 changes: 124 additions & 0 deletions src/backend/tests/unit/recruitment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,130 @@ describe('Recruitment Tests', () => {
).rejects.toThrow(new AccessDeniedAdminOnlyException('create a guest definition'));
});
});
describe('Edit Guest Definition', () => {
it('Fails if user is not an admin', async () => {
await expect(
async () =>
await RecruitmentServices.editGuestDefinition(
await createTestUser(member, orgId),
organization,
'test term',
'test description',
'test definition id',
2,
'buttonTxt',
'buttonLink'
)
).rejects.toThrow(new AccessDeniedAdminOnlyException('edit a guest definition'));
});

it('Fails if guest definition doesn`t exist', async () => {
await expect(
async () =>
await RecruitmentServices.editGuestDefinition(
await createTestUser(batmanAppAdmin, orgId),
organization,
'term',
'description',
'definition id',
2,
'buttonTxt',
'buttonLink'
)
).rejects.toThrow(new NotFoundException('Guest Definition', 'definition id'));
});

it('Successful edit guest definition', async () => {
const def = await RecruitmentServices.createGuestDefinition(
superman,
organization,
'test term',
'test description',
2,
'iconname',
'buttonTxt',
'buttonLink'
);

const edited = await RecruitmentServices.editGuestDefinition(
await createTestUser(batmanAppAdmin, orgId),
organization,
'new term',
'new description',
def.definitionId,
4,
'new icon',
'new text',
'new link'
);

expect(edited.term).toBe('new term');
expect(edited.description).toBe('new description');
expect(edited.order).toBe(4);
expect(edited.icon).toBe('new icon');
expect(edited.buttonText).toBe('new text');
expect(edited.buttonLink).toBe('new link');
});

it('Edit guest definition fails if defintion is deleted', async () => {
const def = await RecruitmentServices.createGuestDefinition(
superman,
organization,
'test term',
'test description',
2,
'iconname',
'buttonTxt',
'buttonLink'
);

const batman = await createTestUser(batmanAppAdmin, orgId);

await RecruitmentServices.deleteGuestDefinition(batman, def.definitionId, organization);

await expect(
async () =>
await RecruitmentServices.editGuestDefinition(
batman,
organization,
'term',
'description',
def.definitionId,
2,
'buttonTxt',
'buttonLink'
)
).rejects.toThrow(new DeletedException('Guest Definition', def.definitionId));
});

it('Fails if milestone is deleted', async () => {
const milestone = await RecruitmentServices.createMilestone(
await createTestUser(batmanAppAdmin, orgId),
'name',
'description',
new Date('11/12/24'),
organization
);

await prisma.milestone.delete({
where: {
milestoneId: milestone.milestoneId
}
});

await expect(
async () =>
await RecruitmentServices.editMilestone(
superman,
'name',
'description',
new Date('11/12/24'),
milestone.milestoneId,
organization
)
).rejects.toThrow(new NotFoundException('Milestone', milestone.milestoneId));
});
});

describe('Delete Guest Definition', () => {
it('Fails if user is not an admin', async () => {
Expand Down
Loading