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
No related branches found
No related tags found
1 merge request!11fix: added and endpoint to post/delete people in the inner groups of a group
......@@ -2,12 +2,14 @@ import {
Injectable,
NotFoundException,
InternalServerErrorException,
BadRequestException,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateGroupDto } from '../dto/create-group.dto';
import { UpdateGroupDto } from '../dto/update-group.dto';
import { Group } from '../schemas/group.schema';
import * as Roles from 'src/shared/Roles';
@Injectable()
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> =>
new Promise((resolve, reject) => {
this._groupModel.findByIdAndDelete(id, {}, (err) => {
......@@ -72,7 +117,7 @@ export class GroupsDao {
findByParentAndRemove = (parent: string): Promise<Group | void> =>
new Promise((resolve, reject) => {
this._groupModel.remove({parent : {$regex : parent}}, (err) => {
this._groupModel.remove({ parent: { $regex: parent } }, (err) => {
if (err) reject(err.message);
resolve();
});
......
......@@ -42,6 +42,19 @@ export class GroupsController {
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(@Param() params: { id: string }): Promise<GroupEntity | void> {
return this._groupsService.delete(params.id);
......
......@@ -19,15 +19,22 @@ export class GroupsService {
update = (id: string, group: UpdateGroupDto): Promise<GroupEntity | void> =>
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> {
return this._groupsDao.findById(id).then(res => {
if(res){
let parent = res.parent + "-" + res.name;
return this._groupsDao.findById(id).then((res) => {
if (res) {
const parent = res.parent + '-' + res.name;
this._groupsDao.findByIdAndRemove(id).then(() => {
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.
Finish editing this message first!
Please register or to comment