users update create delete tested
This commit is contained in:
@@ -44,4 +44,16 @@ export class CreateUserInput {
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
type?: string;
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
expiryStarts?: string;
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
expiryEnds?: string;
|
||||
|
||||
@Field(() => Boolean, { nullable: true })
|
||||
isConfirmed?: boolean;
|
||||
|
||||
@Field(() => Boolean, { nullable: true })
|
||||
isNotificationSend?: boolean;
|
||||
}
|
||||
|
||||
53
backend/src/users/dto/update-user.input.ts
Normal file
53
backend/src/users/dto/update-user.input.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { InputType, Field, ID } from '@nestjs/graphql';
|
||||
|
||||
@InputType()
|
||||
export class UpdateCollectionTokenItemInput {
|
||||
@Field({ nullable: true })
|
||||
prefix?: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
token?: string;
|
||||
}
|
||||
|
||||
@InputType()
|
||||
export class UpdateCollectionTokenInput {
|
||||
@Field(() => [UpdateCollectionTokenItemInput], { nullable: true })
|
||||
tokens?: UpdateCollectionTokenItemInput[];
|
||||
|
||||
@Field({ nullable: true })
|
||||
default?: string;
|
||||
}
|
||||
|
||||
@InputType()
|
||||
export class UpdateUserInput {
|
||||
|
||||
@Field({ nullable: true })
|
||||
tag?: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
email?: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
phone?: string;
|
||||
|
||||
@Field(() => UpdateCollectionTokenInput, { nullable: true })
|
||||
collectionTokens?: UpdateCollectionTokenInput;
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
person?: string;
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
type?: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
expiryStarts?: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
expiryEnds?: string;
|
||||
|
||||
@Field(() => Boolean, { nullable: true })
|
||||
isConfirmed?: boolean;
|
||||
|
||||
@Field(() => Boolean, { nullable: true })
|
||||
isNotificationSend?: boolean;
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import { Resolver, Query, Args, ID, Info, Mutation, Int } from '@nestjs/graphql';
|
||||
import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
|
||||
import { Types } from 'mongoose';
|
||||
import { User } from '@/models/user.model';
|
||||
import { UsersService } from '@/users/users.service';
|
||||
import { CreateUserInput } from './dto/create-user.input';
|
||||
import { ListArguments } from '@/dto/list.input';
|
||||
import { UsersListResponse } from '@/people/dto/list-result.response';
|
||||
import { UpdateUserInput } from './dto/update-user.input';
|
||||
import graphqlFields from 'graphql-fields';
|
||||
import type { GraphQLResolveInfo } from 'graphql';
|
||||
|
||||
@@ -27,4 +28,10 @@ export class UsersResolver {
|
||||
@Mutation(() => User, { name: 'createUser' })
|
||||
async createUser(@Args('input') input: CreateUserInput): Promise<User> { return this.usersService.create(input) }
|
||||
|
||||
@Mutation(() => User, { name: 'updateUser' })
|
||||
async updateUser(@Args('uuid') uuid: string, @Args('input') input: UpdateUserInput): Promise<User> { return this.usersService.update(uuid, input) }
|
||||
|
||||
@Mutation(() => Boolean, { name: 'deleteUser' })
|
||||
async deleteUser(@Args('uuid') uuid: string): Promise<boolean> { return this.usersService.delete(uuid) }
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Types, Model } from 'mongoose';
|
||||
import { User, UserDocument } from '@/models/user.model';
|
||||
import { CreateUserInput } from './dto/create-user.input';
|
||||
import { UsersListResponse } from '@/people/dto/list-result.response';
|
||||
import { UpdateUserInput } from './dto/update-user.input';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
@@ -13,7 +14,7 @@ export class UsersService {
|
||||
async findAll(projection: any, skip: number, limit: number, sort?: Record<string, 1 | -1>, filters?: Record<string, any>): Promise<UsersListResponse> {
|
||||
const query: any = {}; if (filters && Object.keys(filters).length > 0) { Object.assign(query, filters) };
|
||||
const totalCount = await this.userModel.countDocuments(query).exec();
|
||||
const data = await this.userModel.find(query, projection, { lean: true }).skip(skip).limit(limit).sort(sort).exec()
|
||||
const data = await this.userModel.find(query, projection, { lean: true }).skip(skip).limit(limit).sort(sort).exec();
|
||||
return { data, totalCount };
|
||||
}
|
||||
|
||||
@@ -21,8 +22,10 @@ export class UsersService {
|
||||
|
||||
async create(input: CreateUserInput): Promise<UserDocument> { const user = new this.userModel(input); return user.save() }
|
||||
|
||||
buildProjection(fields: Record<string, any>): Record<string, 1> {
|
||||
const projection: Record<string, 1> = {}; for (const key in fields) { projection[key] = 1 }; console.dir({ fields, projection }, { depth: null }); return projection
|
||||
}
|
||||
async update(uuid: string, input: UpdateUserInput): Promise<UserDocument> { const user = await this.userModel.findOne({ uuid }); if (!user) { throw new Error('User not found') }; user.set(input); console.dir({ uuid, input }, { depth: null }); return user.save() }
|
||||
|
||||
async delete(uuid: string): Promise<boolean> { const user = await this.userModel.deleteMany({ uuid }); return user.deletedCount > 0 }
|
||||
|
||||
buildProjection(fields: Record<string, any>): Record<string, 1> { const projection: Record<string, 1> = {}; for (const key in fields) { projection[key] = 1 }; return projection }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user