-
Notifications
You must be signed in to change notification settings - Fork 1
/
userCanEdit.ts
37 lines (31 loc) · 1.14 KB
/
userCanEdit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { isEmpty } from '@infrastructure/utils/empty.js';
import { MemberRole } from '@domain/entities/team.js';
import type { PolicyContext } from '@presentation/http/types/PolicyContext.js';
/**
* Policy to check whether a user has permission to edit the note
* @param context - Context object, containing Fatify request, Fastify reply and domain services
*/
export default async function userCanEdit(context: PolicyContext): Promise<void> {
const { request, reply, domainServices } = context;
const { userId } = request;
/**
* If user is not authorized, we can't check his permissions
*/
if (isEmpty(userId)) {
return await reply.unauthorized();
};
/**
* If note is not resolved, we can't check permissions
*/
if (isEmpty(request.note)) {
return await reply.notAcceptable('Note not found');
};
const memberRole = await domainServices.noteSettingsService.getUserRoleByUserIdAndNoteId(request.userId!, request.note.id);
/**
* If user has a Read Role or is not in team at all,
* he doesn't have permission to edit the note
*/
if (memberRole !== MemberRole.Write) {
return await reply.forbidden();
}
}