From d646f88939285088abe84380c212a52f5950bc95 Mon Sep 17 00:00:00 2001 From: Ivan Alglave <ivanalglave@outlook.fr> Date: Tue, 22 Nov 2022 11:26:57 +0100 Subject: [PATCH] Changed groups format to use unique id and non-unique name --- src/groups/dao/groups.dao.ts | 9 +++--- src/groups/dto/create-group.dto.ts | 2 +- src/groups/entities/group.entity.ts | 2 +- src/groups/groups.controller.ts | 1 - src/groups/groups.types.ts | 1 - src/groups/schemas/group.schema.ts | 4 +-- src/people/dao/people.dao.ts | 44 +++++++++++++++-------------- 7 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/groups/dao/groups.dao.ts b/src/groups/dao/groups.dao.ts index 682c742..01bd11f 100644 --- a/src/groups/dao/groups.dao.ts +++ b/src/groups/dao/groups.dao.ts @@ -27,7 +27,7 @@ export class GroupsDao { findById = (id: string): Promise<Group | void> => new Promise((resolve, reject) => { - this._groupModel.findOne({ id: id }, {}, {}, (err, value) => { + this._groupModel.findById(id, {}, {}, (err, value) => { if (err) reject(err.message); if (!value) reject(new NotFoundException()); resolve(value); @@ -48,8 +48,8 @@ export class GroupsDao { group: UpdateGroupDto, ): Promise<Group | void> => new Promise((resolve, reject) => { - this._groupModel.updateOne( - { id: id }, + this._groupModel.findByIdAndUpdate( + id, group, { new: true, @@ -57,7 +57,6 @@ export class GroupsDao { }, (err, value) => { if (err) reject(err.message); - if (value.matchedCount === 0) reject(new NotFoundException()); resolve(value); }, ); @@ -65,7 +64,7 @@ export class GroupsDao { findByIdAndRemove = (id: string): Promise<Group | void> => new Promise((resolve, reject) => { - this._groupModel.deleteOne({ id: id }, {}, (err) => { + this._groupModel.findByIdAndDelete(id, {}, (err) => { if (err) reject(err.message); resolve(); }); diff --git a/src/groups/dto/create-group.dto.ts b/src/groups/dto/create-group.dto.ts index 2444a73..b1156ab 100644 --- a/src/groups/dto/create-group.dto.ts +++ b/src/groups/dto/create-group.dto.ts @@ -3,7 +3,7 @@ import { IsBoolean, IsString, IsNotEmpty } from 'class-validator'; export class CreateGroupDto { @IsString() @IsNotEmpty() - id: string; + name: string; @IsBoolean() final: boolean; diff --git a/src/groups/entities/group.entity.ts b/src/groups/entities/group.entity.ts index b9182cd..6bfc55a 100644 --- a/src/groups/entities/group.entity.ts +++ b/src/groups/entities/group.entity.ts @@ -2,7 +2,7 @@ import { Group } from '../schemas/group.schema'; export class GroupEntity { _id: string; - id: string; + name: string; final: boolean; responsibles: string[]; secretaries: string[]; diff --git a/src/groups/groups.controller.ts b/src/groups/groups.controller.ts index 2531433..662eb3e 100644 --- a/src/groups/groups.controller.ts +++ b/src/groups/groups.controller.ts @@ -8,7 +8,6 @@ import { Body, UseInterceptors, } from '@nestjs/common'; -import { Observable } from 'rxjs'; import { HttpInterceptor } from '../interceptors/http.interceptor'; import { CreateGroupDto } from './dto/create-group.dto'; import { UpdateGroupDto } from './dto/update-group.dto'; diff --git a/src/groups/groups.types.ts b/src/groups/groups.types.ts index ddbd330..879ecc8 100644 --- a/src/groups/groups.types.ts +++ b/src/groups/groups.types.ts @@ -1,6 +1,5 @@ export type Group = { _id: any; - id: string; final: boolean; responsibles: string[]; secretaries: string[]; diff --git a/src/groups/schemas/group.schema.ts b/src/groups/schemas/group.schema.ts index 93698e9..0aa97ba 100644 --- a/src/groups/schemas/group.schema.ts +++ b/src/groups/schemas/group.schema.ts @@ -24,7 +24,7 @@ export class Group { required: true, trim: true, }) - id: string; + name: string; @Prop({ type: Boolean, @@ -63,5 +63,3 @@ export class Group { } export const GroupSchema = SchemaFactory.createForClass(Group); - -GroupSchema.index({ id: 1 }, { unique: true }); diff --git a/src/people/dao/people.dao.ts b/src/people/dao/people.dao.ts index 7669cf6..39dc137 100644 --- a/src/people/dao/people.dao.ts +++ b/src/people/dao/people.dao.ts @@ -1,4 +1,8 @@ -import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'; +import { + Injectable, + InternalServerErrorException, + NotFoundException, +} from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { randomInt } from 'crypto'; import { Model } from 'mongoose'; @@ -31,9 +35,8 @@ export class PeopleDao { }); }); - save (people: CreatePeopleDto): Promise<People> { - var password = this.secret(); - people.passwordHash = password; + save = (people: CreatePeopleDto): Promise<People> => { + people.passwordHash = this.secret(); return new Promise((resolve, reject) => { new this._peopleModel(people).save((err, value) => { if (err) reject(err.message); @@ -41,7 +44,7 @@ export class PeopleDao { resolve(value); }); }); - } + }; findByIdAndUpdate = ( id: string, @@ -71,20 +74,19 @@ export class PeopleDao { }); }); - secret = (length = 10) => { - const upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - const lowerCase = "abcdefghijklmnopqrstuvwxyz"; - const digits = "0123456789"; - const minus = "-"; - const underLine = "_"; - const special = "!\"#$%&'*+,./:;=?@\\^`|~"; - const brackets = "[]{}()<>"; - const alphabet = - upperCase + lowerCase + digits + minus + underLine + special + brackets; - let secret = ""; - for (let index = 0; index < length; index++) - secret += alphabet.charAt(randomInt(alphabet.length)); - return secret; - }; - + secret = (length = 10) => { + const upperCase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + const lowerCase = 'abcdefghijklmnopqrstuvwxyz'; + const digits = '0123456789'; + const minus = '-'; + const underLine = '_'; + const special = '!"#$%&\'*+,./:;=?@\\^`|~'; + const brackets = '[]{}()<>'; + const alphabet = + upperCase + lowerCase + digits + minus + underLine + special + brackets; + let secret = ''; + for (let index = 0; index < length; index++) + secret += alphabet.charAt(randomInt(alphabet.length)); + return secret; + }; } -- GitLab