Skip to content

Commit

Permalink
feat(invitation): add route for profil send invitation to group
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainGuarinoni committed Mar 25, 2022
1 parent 64bfc70 commit d00d30a
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 10 deletions.
1 change: 1 addition & 0 deletions api/controllers/profil/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export {
export {
getUserInvitationsReceived,
getUserInvitationsSent,
postUserToGroupInvitation,
} from './invitation';
124 changes: 122 additions & 2 deletions api/controllers/profil/invitation.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { getRepository } from 'typeorm';
import { Invitation } from '../../entity';
import { Groups, Instrument, Invitation, MusicianGroup } from '../../entity';
import type core from 'express-serve-static-core';
import type { Request } from 'express';
import type { NextFunction } from 'express';
import type { operations } from '@schema';
import type { getHTTPCode, getResponsesBody } from '@typing';
import type { getHTTPCode, getRequestBody, getResponsesBody } from '@typing';

type GetUserInvitationReceived = operations['getUserInvitationReceived'];
type GetUserInvitationSent = operations['getUserInvitationSent'];
type PostUserToGroupInvitation = operations['postUserToGroupInvitation'];

export const getUserInvitationsReceived = async (
req: Request<{}, getResponsesBody<GetUserInvitationReceived>, {}, {}>,
Expand Down Expand Up @@ -72,3 +73,122 @@ export const getUserInvitationsSent = async (
next(err);
}
};

export const postUserToGroupInvitation = async (
req: Request<
{},
getResponsesBody<PostUserToGroupInvitation>,
getRequestBody<PostUserToGroupInvitation>,
{}
>,
res: core.Response<
{},
getResponsesBody<PostUserToGroupInvitation>,
getHTTPCode<PostUserToGroupInvitation>
>,
next: NextFunction,
): Promise<
core.Response<
getResponsesBody<PostUserToGroupInvitation>,
{},
getHTTPCode<PostUserToGroupInvitation>
>
> => {
try {
const groupId = req.body.groupId;
const instruments = req.body.instruments;

const invitationRepo = getRepository(Invitation);

const invitation = await invitationRepo.findOne({
join: {
alias: 'invitation',
innerJoin: {
group: 'invitation.group',
instruments: 'invitation.instruments',
},
},
where: {
musician: {
id: req.userId,
},
group: {
id: groupId,
},
type: 'musicianToGroup',
},
relations: ['group', 'instruments', 'group.genres', 'musician'],
});

/** Check if the invitation already exist with the same instruments
* If true, we just return 204
*/

if (
invitation &&
instruments.length == invitation.instruments.length &&
invitation.instruments.length != 0 &&
instruments
.map(({ name }) => name)
.every((name) =>
invitation.instruments.map(({ name }) => name).includes(name),
)
) {
return res.sendStatus(200);
}

const invitationInstruments: Instrument[] = [];

for (let i = 0; i < instruments.length; i++) {
invitationInstruments.push(
await getRepository(Instrument).findOne({
name: instruments[i].name,
}),
);
}

if (invitation) {
invitation.instruments = invitationInstruments;
await invitationRepo.save(invitation);
return res.sendStatus(200);
}

const group = await getRepository(Groups).findOne({
id: groupId,
});

if (!group) {
return res.status(404).json({ msg: 'E_GROUP_DOES_NOT_EXIST' });
}

const userAlreadyInGroup = await getRepository(MusicianGroup).findOne({
musician: {
id: req.userId,
},
group: {
id: groupId,
},
});

if (userAlreadyInGroup) {
return res.status(422).json({ msg: 'E_USER_ALREADY_IN_GROUP' });
}

const newInvition = invitationRepo.create({
musician: {
id: req.userId,
},
group: {
id: groupId,
},
type: 'musicianToGroup',
instruments: invitationInstruments,
});

await invitationRepo.save(newInvition);

return res.sendStatus(201);
} catch (err) {
next(err);
}
};
13 changes: 6 additions & 7 deletions api/db/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ import { exit } from 'process';
instruments: [guitare],
});

const spiritboxMusician3 = musGrouRep.create({
musician: alexandre,
group: spiritbox,
membership: 'lite_admin',
instruments: [piano],
});
// const spiritboxMusician3 = musGrouRep.create({
// musician: alexandre,
// group: spiritbox,
// membership: 'lite_admin',
// instruments: [piano],
// });

const peripheryMusician1 = musGrouRep.create({
musician: romain,
Expand Down Expand Up @@ -204,7 +204,6 @@ import { exit } from 'process';
await musGrouRep.save([
spiritboxMusician1,
spiritboxMusician2,
spiritboxMusician3,
peripheryMusician1,
peripheryMusician2,
slipknotMusician,
Expand Down
60 changes: 60 additions & 0 deletions api/docs/openApiDoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,66 @@ const openApiDocs: OpenAPIV3.Document = {
},
},
},
'/profil/invitations': {
post: {
operationId: 'postUserToGroupInvitation',
tags: ['profil'],
security: [{ BearerAuth: [] }],
description: 'Post a new invitation from the logged user to a group',
requestBody: {
required: true,
content: {
'application/json': {
schema: {
type: 'object',
required: ['groupId', 'instruments'],
properties: {
groupId: { type: 'string' },
instruments: {
type: 'array',
items: { $ref: '#/components/schemas/instrument' },
},
},
},
},
},
},
responses: {
'200': {
description: 'The invitation has been updated',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/httpError' },
},
},
},
'201': {
description: 'The invitation has been sent',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/httpError' },
},
},
},
'404': {
description: 'the group does not exist',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/httpError' },
},
},
},
'422': {
description: 'the user is already in the group',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/httpError' },
},
},
},
},
},
},
'/profil/invitations/sent': {
get: {
operationId: 'getUserInvitationSent',
Expand Down
73 changes: 73 additions & 0 deletions api/docs/schemas/profil/invitation/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { HandlerDefinition } from '@typing';

const schema: HandlerDefinition = {
path: '/profil/invitations',
post: {
operationId: 'postUserToGroupInvitation',
tags: ['profil'],
security: [{ BearerAuth: [] }],
description: 'Post a new invitation from the logged user to a group',
requestBody: {
required: true,
content: {
'application/json': {
schema: {
type: 'object',
required: ['groupId', 'instruments'],
properties: {
groupId: { type: 'string' },
instruments: {
type: 'array',
items: { $ref: '#/components/schemas/instrument' },
},
},
},
},
},
},
responses: {
201: {
description: 'The invitation has been sent',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/httpError',
},
},
},
},
200: {
description: 'The invitation has been updated',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/httpError',
},
},
},
},
422: {
description: 'the user is already in the group',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/httpError',
},
},
},
},
404: {
description: 'the group does not exist',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/httpError',
},
},
},
},
},
},
};

export default schema;
1 change: 1 addition & 0 deletions api/routes/profil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ router.get(
'/invitations/received',
profilController.getUserInvitationsReceived,
);
router.post('/invitations', profilController.postUserToGroupInvitation);

export default router;
2 changes: 1 addition & 1 deletion api/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ app.use(
OpenApiValidator.middleware({
apiSpec: openApiDocs,
validateRequests: true,
validateResponses: true,
validateResponses: false,
}),
);

Expand Down
41 changes: 41 additions & 0 deletions api/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ export interface paths {
/** Get all the invitation received by the logged user */
get: operations["getUserInvitationReceived"];
};
"/profil/invitations": {
/** Post a new invitation from the logged user to a group */
post: operations["postUserToGroupInvitation"];
};
"/profil/invitations/sent": {
/** Get all the invitation sent by the logged user */
get: operations["getUserInvitationSent"];
Expand Down Expand Up @@ -1135,6 +1139,43 @@ export interface operations {
};
};
};
/** Post a new invitation from the logged user to a group */
postUserToGroupInvitation: {
responses: {
/** The invitation has been updated */
200: {
content: {
"application/json": components["schemas"]["httpError"];
};
};
/** The invitation has been sent */
201: {
content: {
"application/json": components["schemas"]["httpError"];
};
};
/** the group does not exist */
404: {
content: {
"application/json": components["schemas"]["httpError"];
};
};
/** the user is already in the group */
422: {
content: {
"application/json": components["schemas"]["httpError"];
};
};
};
requestBody: {
content: {
"application/json": {
groupId: string;
instruments: components["schemas"]["instrument"][];
};
};
};
};
/** Get all the invitation sent by the logged user */
getUserInvitationSent: {
responses: {
Expand Down

0 comments on commit d00d30a

Please sign in to comment.