-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjoin.ts
More file actions
75 lines (67 loc) · 1.7 KB
/
join.ts
File metadata and controls
75 lines (67 loc) · 1.7 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { FastifyPluginCallback } from 'fastify';
import type NoteSettingsService from '@domain/service/noteSettings.js';
import type { TeamMember } from '@domain/entities/team.js';
/**
* Represents AI router options
*/
interface JoinRouterOptions {
/**
* Note settings service instance
*/
noteSettings: NoteSettingsService;
}
/**
* Join Router plugin
*
* @todo use different replies for different errors in post route
* @todo add check for write permission in route
* @param fastify - fastify instance
* @param opts - router options
* @param done - done callback
*/
const JoinRouter: FastifyPluginCallback<JoinRouterOptions> = (fastify, opts, done) => {
const noteSettingsService = opts.noteSettings;
fastify.post<{
Body: {
hash: string
},
}>('/:hash', {
schema: {
body: {
hash: {
$ref: 'JoinSchemaParams#/properties/hash',
},
},
response: {
'2xx': {
description: 'Team member',
content: {
'application/json': {
schema: {
$ref: 'JoinSchemaResponse#/properties',
},
},
},
},
},
},
config: {
policy: [
'authRequired',
],
},
}, async (request, reply) => {
const { hash } = request.body;
const { userId } = request;
let result: TeamMember | null = null;
try {
result = await noteSettingsService.addUserToTeamByInvitationHash(hash, userId as number);
} catch (error: unknown) {
const causedError = error as Error;
return reply.notAcceptable(causedError.message);
}
return reply.send({ result });
});
done();
};
export default JoinRouter;