Skip to content
Snippets Groups Projects
Commit 7881959c authored by Ivan Alglave's avatar Ivan Alglave
Browse files

fix: added and endpoint to post/delete people in the inner groups of a group

parent a1a92094
Branches groups-nested-crud
No related tags found
Loading
This commit is part of merge request !11. Comments created here will be created in the context of that merge request.
...@@ -2,12 +2,14 @@ import { ...@@ -2,12 +2,14 @@ import {
Injectable, Injectable,
NotFoundException, NotFoundException,
InternalServerErrorException, InternalServerErrorException,
BadRequestException,
} from '@nestjs/common'; } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose'; import { Model } from 'mongoose';
import { CreateGroupDto } from '../dto/create-group.dto'; import { CreateGroupDto } from '../dto/create-group.dto';
import { UpdateGroupDto } from '../dto/update-group.dto'; import { UpdateGroupDto } from '../dto/update-group.dto';
import { Group } from '../schemas/group.schema'; import { Group } from '../schemas/group.schema';
import * as Roles from 'src/shared/Roles';
@Injectable() @Injectable()
export class GroupsDao { export class GroupsDao {
...@@ -62,6 +64,49 @@ export class GroupsDao { ...@@ -62,6 +64,49 @@ export class GroupsDao {
); );
}); });
findByIdAndUpdateRole = (
id: string,
role: string,
personId: string,
action: string,
): Promise<Group | void> =>
new Promise((resolve, reject) => {
let query = {};
let queryInner = {};
switch (role) {
case Roles.ROLE_RESPONSIBLE:
queryInner = { responsibles: personId };
break;
case Roles.ROLE_SECRETARY:
queryInner = { secretaries: personId };
break;
case Roles.ROLE_STUDENT:
queryInner = { students: personId };
break;
default:
reject(new BadRequestException('Bad role'));
}
switch (action) {
case 'post':
query = { $push: queryInner };
break;
case 'delete':
query = { $pull: queryInner };
break;
default:
reject(new BadRequestException('Bad action'));
}
this._groupModel.findByIdAndUpdate(
id,
query,
{ new: true },
(err, value) => {
if (err) reject(err.message);
resolve(value);
},
);
});
findByIdAndRemove = (id: string): Promise<Group | void> => findByIdAndRemove = (id: string): Promise<Group | void> =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
this._groupModel.findByIdAndDelete(id, {}, (err) => { this._groupModel.findByIdAndDelete(id, {}, (err) => {
...@@ -72,7 +117,7 @@ export class GroupsDao { ...@@ -72,7 +117,7 @@ export class GroupsDao {
findByParentAndRemove = (parent: string): Promise<Group | void> => findByParentAndRemove = (parent: string): Promise<Group | void> =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
this._groupModel.remove({parent : {$regex : parent}}, (err) => { this._groupModel.remove({ parent: { $regex: parent } }, (err) => {
if (err) reject(err.message); if (err) reject(err.message);
resolve(); resolve();
}); });
......
...@@ -42,6 +42,19 @@ export class GroupsController { ...@@ -42,6 +42,19 @@ export class GroupsController {
return this._groupsService.update(params.id, updateGroupDto); return this._groupsService.update(params.id, updateGroupDto);
} }
@Put(':id/:role')
updateOneByRole(
@Param() params: { id: string; role: string },
@Body() body: { personId: string; action: string },
): Promise<GroupEntity | void> {
return this._groupsService.updateOneByRole(
params.id,
params.role,
body.personId,
body.action,
);
}
@Delete(':id') @Delete(':id')
delete(@Param() params: { id: string }): Promise<GroupEntity | void> { delete(@Param() params: { id: string }): Promise<GroupEntity | void> {
return this._groupsService.delete(params.id); return this._groupsService.delete(params.id);
......
...@@ -19,15 +19,22 @@ export class GroupsService { ...@@ -19,15 +19,22 @@ export class GroupsService {
update = (id: string, group: UpdateGroupDto): Promise<GroupEntity | void> => update = (id: string, group: UpdateGroupDto): Promise<GroupEntity | void> =>
this._groupsDao.findByIdAndUpdate(id, group); this._groupsDao.findByIdAndUpdate(id, group);
updateOneByRole = (
id: string,
role: string,
personId: string,
action: string,
): Promise<GroupEntity | void> =>
this._groupsDao.findByIdAndUpdateRole(id, role, personId, action);
delete(id: string): Promise<GroupEntity | void> { delete(id: string): Promise<GroupEntity | void> {
return this._groupsDao.findById(id).then(res => { return this._groupsDao.findById(id).then((res) => {
if(res){ if (res) {
let parent = res.parent + "-" + res.name; const parent = res.parent + '-' + res.name;
this._groupsDao.findByIdAndRemove(id).then(() => { this._groupsDao.findByIdAndRemove(id).then(() => {
this._groupsDao.findByParentAndRemove(parent); this._groupsDao.findByParentAndRemove(parent);
}) });
} }
}) });
} }
} }
export const ROLE_STUDENT = 'student';
export const ROLE_SECRETARY = 'secretary';
export const ROLE_RESPONSIBLE = 'responsible';
export const ROLE_ADMIN = 'admin';
export const ROLES = [
ROLE_STUDENT,
ROLE_SECRETARY,
ROLE_RESPONSIBLE,
ROLE_ADMIN,
];
export const isRoleValid = (potentialRole: string): boolean =>
ROLES.includes(potentialRole);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment