Skip to content

Commit 2c4d946

Browse files
committed
WIP rename user schema to entity
1 parent 72dcd76 commit 2c4d946

File tree

16 files changed

+201
-91
lines changed

16 files changed

+201
-91
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export enum EntityType {
2+
CONSOLE = 0,
3+
OPERATOR = 1,
4+
AGENT = 2,
5+
CLIENT = 3,
6+
COMPANY = 4,
7+
OTHER = 99,
8+
}
9+
10+
export const EntityTypeList: number[] = Object.keys(EntityType)
11+
.filter((k) => typeof EntityType[k as any] === 'number')
12+
.map((k) => parseInt(EntityType[k as any], 10))

service/src/_common/enum/user-type.enum.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

service/src/_common/schemas/parts/user.part.schema.ts renamed to service/src/_common/schemas/parts/entity.part.schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
22
import { IdnamePart } from '~/_common/schemas/parts/idname.part.schema'
3-
import { UserTypeList } from '~/_common/enum/user-type.enum'
3+
import { EntityTypeList } from '~/_common/enum/entity-type.enum'
44

55
@Schema({ _id: false })
6-
export class UserPart extends IdnamePart {
6+
export class EntityPart extends IdnamePart {
77
@Prop({
88
type: Number,
9-
enum: UserTypeList,
9+
enum: EntityTypeList,
1010
required: true,
1111
})
1212
public type: number
1313
}
1414

15-
export const UserPartSchema = SchemaFactory.createForClass(UserPart)
15+
export const EntityPartSchema = SchemaFactory.createForClass(EntityPart)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { PartialType } from '@nestjs/swagger'
2+
3+
export class EntitiesCreateDto {
4+
5+
}
6+
7+
export class EntitiesUpdateDto extends PartialType(EntitiesCreateDto) {
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
2+
import { AbstractSchema } from '~/_common/abstracts/schemas/abstract.schema'
3+
import { ProfilePart, ProfilePartSchema } from '~/core/entites/_schemas/parts/profile.part.schema'
4+
5+
@Schema({
6+
collection: 'entites',
7+
versionKey: false,
8+
})
9+
export class Entity extends AbstractSchema {
10+
@Prop({
11+
type: String,
12+
required: true,
13+
})
14+
public publicEmail: string
15+
16+
@Prop({
17+
type: ProfilePartSchema,
18+
required: true,
19+
})
20+
public profile: ProfilePart
21+
}
22+
23+
export const EntitySchema = SchemaFactory.createForClass(Entity)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
2+
import { Document } from 'mongoose'
3+
4+
@Schema({ _id: false })
5+
export class ProfilePart extends Document {
6+
@Prop({
7+
type: String,
8+
required: true,
9+
})
10+
public firstName: string
11+
12+
@Prop({
13+
type: String,
14+
required: true,
15+
})
16+
public lastName: string
17+
18+
@Prop({
19+
type: String,
20+
})
21+
public thirdName?: string
22+
23+
@Prop({
24+
type: String,
25+
})
26+
public avatar?: string
27+
}
28+
29+
export const ProfilePartSchema = SchemaFactory.createForClass(ProfilePart)
30+
ProfilePartSchema.virtual('commonName').get(function (this: ProfilePart): string {
31+
const commonNamePart = [this.firstName]
32+
if (this.thirdName) commonNamePart.push(this.thirdName)
33+
commonNamePart.push(this.lastName)
34+
return commonNamePart.join(' ')
35+
})

service/src/core/users/users.controller.spec.ts renamed to service/src/core/entites/entites.controller.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { DeleteResult } from 'mongodb'
22
import { Test, TestingModule } from '@nestjs/testing'
3-
import { UsersController } from './users.controller'
4-
import { UsersService } from './users.service'
5-
import { UsersDto } from './dto/users.dto'
6-
import { Users } from './schemas/users.schema'
3+
import { EntitesController } from './entites.controller'
4+
import { EntitesService } from './entites.service'
5+
import { UsersDto } from '~/core/entites/_dto/entites.dto'
6+
import { Users } from '~/core/entites/_schemas/entites.schema'
77
import { HttpException, HttpStatus } from '@nestjs/common'
88
import { Types } from 'mongoose'
99
import { Response, Request } from 'express'
1010
import { getMockReq, getMockRes } from '@jest-mock/express'
1111

1212
describe('UsersController', () => {
13-
let controller: UsersController
14-
let service: UsersService
13+
let controller: EntitesController
14+
let service: EntitesService
1515
const date = new Date()
1616
const _id = new Types.ObjectId()
1717
const { res, mockClear } = getMockRes()
@@ -29,11 +29,11 @@ describe('UsersController', () => {
2929
beforeEach(async () => {
3030
mockClear()
3131
const module: TestingModule = await Test.createTestingModule({
32-
controllers: [UsersController],
32+
controllers: [EntitesController],
3333
providers: [
34-
UsersService,
34+
EntitesService,
3535
{
36-
provide: UsersService,
36+
provide: EntitesService,
3737
useValue: {
3838
search: jest.fn().mockResolvedValue([[object], 1]),
3939
create: jest.fn().mockResolvedValue(object),
@@ -48,8 +48,8 @@ describe('UsersController', () => {
4848
],
4949
}).compile()
5050

51-
controller = module.get<UsersController>(UsersController)
52-
service = module.get<UsersService>(UsersService)
51+
controller = module.get<EntitesController>(EntitesController)
52+
service = module.get<EntitesService>(EntitesService)
5353
})
5454

5555
describe('search', () => {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Body, Controller, Delete, Get, HttpStatus, Param, Patch, Post, Req, Res } from '@nestjs/common'
2+
import { EntitesService } from './entites.service'
3+
import { AbstractController } from '~/_common/abstracts/abstract.controller'
4+
import { Request, Response } from 'express'
5+
import { FilterOptions, FilterSchema, SearchFilterOptions, SearchFilterSchema } from '@streamkits/nestjs_module_scrud'
6+
import { ApiParam } from '@nestjs/swagger'
7+
import { ObjectIdValidationPipe } from '~/_common/pipes/object-id-validation.pipe'
8+
import { Types } from 'mongoose'
9+
import { EntitiesCreateDto, EntitiesUpdateDto } from '~/core/entites/_dto/entites.dto'
10+
11+
@Controller('entites')
12+
export class EntitesController extends AbstractController {
13+
protected readonly projection = {
14+
name: 1,
15+
color: 1,
16+
icon: 1,
17+
}
18+
19+
public constructor(private readonly _service: EntitesService) {
20+
super()
21+
}
22+
23+
@Post()
24+
public async create(@Req() req: Request, @Res() res: Response, @Body() body: EntitiesCreateDto) {
25+
const data = await this._service.create(body)
26+
return res.status(HttpStatus.CREATED).json({
27+
statusCode: HttpStatus.CREATED,
28+
data,
29+
})
30+
}
31+
32+
@Get()
33+
public async search(@Res() res: Response, @SearchFilterSchema() searchFilterSchema: FilterSchema, @SearchFilterOptions() searchFilterOptions: FilterOptions) {
34+
const [data, total] = await this._service.findAndCount(searchFilterSchema, this.projection, searchFilterOptions)
35+
return res.status(HttpStatus.OK).json({
36+
statusCode: HttpStatus.OK,
37+
total,
38+
data,
39+
})
40+
}
41+
42+
@Get(':_id([0-9a-fA-F]{24})')
43+
@ApiParam({ name: '_id', type: String })
44+
public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) {
45+
const data = await this._service.findById(_id)
46+
return res.status(HttpStatus.OK).json({
47+
statusCode: HttpStatus.OK,
48+
data,
49+
})
50+
}
51+
52+
@Patch(':_id([0-9a-fA-F]{24})')
53+
@ApiParam({ name: '_id', type: String })
54+
public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: EntitiesUpdateDto, @Res() res: Response) {
55+
const data = await this._service.update(_id, body)
56+
return res.status(HttpStatus.OK).json({
57+
statusCode: HttpStatus.OK,
58+
data,
59+
})
60+
}
61+
62+
@Delete(':_id([0-9a-fA-F]{24})')
63+
@ApiParam({ name: '_id', type: String })
64+
public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) {
65+
const data = await this._service.delete(_id)
66+
return res.status(HttpStatus.OK).json({
67+
statusCode: HttpStatus.OK,
68+
data,
69+
})
70+
}
71+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Module } from '@nestjs/common'
2+
import { MongooseModule } from '@nestjs/mongoose'
3+
import { Entity, EntitySchema } from '~/core/entites/_schemas/entites.schema'
4+
import { EntitesService } from './entites.service'
5+
import { EntitesController } from './entites.controller'
6+
7+
@Module({
8+
imports: [
9+
MongooseModule.forFeatureAsync([
10+
{
11+
name: Entity.name,
12+
useFactory: () => EntitySchema,
13+
},
14+
]),
15+
],
16+
providers: [EntitesService],
17+
controllers: [EntitesController],
18+
})
19+
export class EntitesModule {}

service/src/core/users/users.service.spec.ts renamed to service/src/core/entites/entites.service.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { DeleteResult } from 'mongodb'
22
import { Test, TestingModule } from '@nestjs/testing'
3-
import { UsersService } from './users.service'
3+
import { EntitesService } from './entites.service'
44
import { getModelToken } from '@nestjs/mongoose'
5-
import { Users } from './schemas/users.schema'
5+
import { Users } from '~/core/entites/_schemas/entites.schema'
66
import { Model, Types } from 'mongoose'
77

88
describe('UsersService', () => {
9-
let service: UsersService
9+
let service: EntitesService
1010
let model: Model<Users>
1111
const _id = new Types.ObjectId()
1212
const date = new Date()
@@ -24,14 +24,14 @@ describe('UsersService', () => {
2424
beforeEach(async () => {
2525
const module: TestingModule = await Test.createTestingModule({
2626
providers: [
27-
UsersService,
27+
EntitesService,
2828
{
2929
provide: getModelToken(Users.name),
3030
useValue: Model,
3131
},
3232
],
3333
}).compile()
34-
service = module.get<UsersService>(UsersService)
34+
service = module.get<EntitesService>(EntitesService)
3535
model = module.get<Model<Users>>(getModelToken(Users.name))
3636
})
3737

0 commit comments

Comments
 (0)