user type graphql added

This commit is contained in:
2025-11-25 20:16:11 +03:00
parent eedfed1a65
commit 2062aa7a1d
52 changed files with 3111 additions and 45 deletions

View File

@@ -3,6 +3,7 @@ import { randomUUID } from 'crypto';
import { Prop, SchemaFactory } from '@nestjs/mongoose';
import { Field, ID, ObjectType } from '@nestjs/graphql';
import { Base } from '@/models/base.model';
import { Build } from '@/models/build.model';
import { BuildParts } from '@/models/build-parts.model';
import { Person } from '@/models/person.model';
import { Company } from '@/models/company.model';
@@ -11,10 +12,18 @@ import { UserType } from '@/models/user-type.model';
@ObjectType()
export class LivingSpaces extends Base {
@Field(() => ID)
@Prop({ type: Types.ObjectId, ref: Build.name, required: true })
build: Types.ObjectId;
@Field(() => ID)
@Prop({ type: Types.ObjectId, ref: BuildParts.name, required: true })
part: Types.ObjectId;
@Field(() => ID)
@Prop({ type: Types.ObjectId, ref: UserType.name, required: true })
userType: Types.ObjectId;
@Field(() => ID)
@Prop({ type: Types.ObjectId, ref: Company.name, required: true })
company: Types.ObjectId;
@@ -23,10 +32,6 @@ export class LivingSpaces extends Base {
@Prop({ type: Types.ObjectId, ref: Person.name, required: true })
person: Types.ObjectId;
@Field(() => ID)
@Prop({ type: Types.ObjectId, ref: UserType.name, required: true })
userType: Types.ObjectId;
}
export type LivingSpacesDocument = LivingSpaces & Document;
@@ -36,7 +41,5 @@ export function getDynamicLivingSpaceModel(collectionToken: string, conn?: Conne
const connection = conn || defaultConnection;
const collectionName = `LivingSpaces${collectionToken}`;
if (connection.models[collectionName]) { return connection.models[collectionName] as Model<LivingSpacesDocument> }
const schema = SchemaFactory.createForClass(LivingSpaces);
schema.add({ uuid: { type: String, required: false, unique: true, default: () => randomUUID() } });
return connection.model(collectionName, schema, collectionName) as unknown as Model<LivingSpacesDocument>;
throw new Error('No Living Spaces is found')
}

View File

@@ -1,25 +1,35 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { ObjectType, Field, ID } from '@nestjs/graphql';
import { Base } from './base.model';
@ObjectType()
@Schema({ timestamps: true })
export class UserType {
@Field()
export class UserType extends Base {
@Field(() => ID)
readonly _id: string
@Field({ nullable: false })
@Prop({ required: true })
type: string;
@Field()
@Field({ nullable: false })
@Prop({ required: true })
token: string;
@Field()
@Field({ nullable: false })
@Prop({ required: true })
typeToken: string;
@Field()
@Field({ nullable: false })
@Prop({ required: true })
description: string;
@Field({ nullable: false })
@Prop({ required: true })
isProperty: boolean;
}
export type UserTypeDocument = UserType & Document;

View File

@@ -3,7 +3,6 @@ import { ObjectType, Field, ID } from '@nestjs/graphql';
import { Document, Types } from 'mongoose';
import { Base } from '@/models/base.model';
import { Person } from '@/models/person.model';
import { UserType } from '@/models/user-type.model';
@ObjectType()
export class CollectionTokenItem {

View File

@@ -1,9 +1,22 @@
import { ExpiryBaseInput } from '@/models/base.model';
import { InputType, Field } from '@nestjs/graphql';
@InputType()
export class CreateUserTypesInput {
export class CreateUserTypesInput extends ExpiryBaseInput {
@Field()
uuid: string;
@Field({ nullable: false })
type: string;
@Field({ nullable: false })
token: string;
@Field({ nullable: false })
typeToken: string;
@Field({ nullable: false })
description: string;
@Field({ nullable: false })
isProperty: boolean;
}

View File

@@ -0,0 +1,13 @@
import { ObjectType, Field } from "@nestjs/graphql";
import { UserType } from "@/models/user-type.model";
@ObjectType()
export class ListUserTypeResponse {
@Field(() => [UserType], { nullable: true })
data: UserType[];
@Field(() => Number, { nullable: true })
totalCount: number;
}

View File

@@ -0,0 +1,22 @@
import { InputType, Field } from "@nestjs/graphql";
import { ExpiryBaseInput } from "@/models/base.model";
@InputType()
export class UpdateUserTypeInput extends ExpiryBaseInput {
@Field({ nullable: true })
type?: string;
@Field({ nullable: true })
token?: string;
@Field({ nullable: true })
typeToken?: string;
@Field({ nullable: true })
description?: string;
@Field({ nullable: true })
isProperty?: boolean;
}

View File

@@ -2,28 +2,36 @@ import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
import { Types } from 'mongoose';
import { UserType } from '@/models/user-type.model';
import { UserTypesService } from '@/user-types/user-types.service';
import { CreateUserTypesInput } from './dto/create-user-types.input';
import { ListUserTypeResponse } from './dto/list-build-sites.response';
import { UpdateUserTypeInput } from './dto/update-build-sites.input';
import { ListArguments } from '@/dto/list.input';
import type { GraphQLResolveInfo } from 'graphql';
import graphqlFields from 'graphql-fields';
import { CreateUserTypesInput } from './dto/create-user-types.input';
@Resolver()
export class UserTypesResolver {
constructor(private readonly userTypesService: UserTypesService) { }
@Query(() => [UserType], { name: 'userTypes' })
async getUsers(@Info() info: GraphQLResolveInfo): Promise<UserType[]> {
const fields = graphqlFields(info); const projection = this.userTypesService.buildProjection(fields); return this.userTypesService.findAll(projection);
@Query(() => ListUserTypeResponse, { name: 'getUserTypes' })
async getUserTypes(@Info() info: GraphQLResolveInfo, @Args('input') input: ListArguments): Promise<ListUserTypeResponse> {
const fields = graphqlFields(info); const projection = this.userTypesService.buildProjection(fields?.data); const { skip, limit, sort, filters } = input;
return await this.userTypesService.findAll(projection, skip, limit, sort, filters);
}
@Query(() => UserType, { name: 'userType', nullable: true })
@Query(() => UserType, { name: 'getUserType', nullable: true })
async getUserType(@Args('id', { type: () => ID }) id: string, @Info() info: GraphQLResolveInfo): Promise<UserType | null> {
const fields = graphqlFields(info); const projection = this.userTypesService.buildProjection(fields); return this.userTypesService.findById(new Types.ObjectId(id), projection);
}
@Mutation(() => UserType, { name: 'createUserType' })
async createUserType(@Args('input') input: CreateUserTypesInput): Promise<UserType> {
return this.userTypesService.create(input);
}
async createUserType(@Args('input') input: CreateUserTypesInput): Promise<UserType> { return this.userTypesService.create(input) }
@Mutation(() => UserType, { name: 'updateUserType' })
async updateUserType(@Args('uuid') uuid: string, @Args('input') input: UpdateUserTypeInput): Promise<UserType> { return this.userTypesService.update(uuid, input) }
@Mutation(() => Boolean, { name: 'deleteUserType' })
async deleteUserType(@Args('uuid') uuid: string): Promise<boolean> { return this.userTypesService.delete(uuid) }
}

View File

@@ -1,8 +1,10 @@
import { Injectable } from '@nestjs/common';
import { CreateUserTypesInput } from './dto/create-user-types.input';
import { InjectModel } from '@nestjs/mongoose';
import { Types, Model } from 'mongoose';
import { UserType, UserTypeDocument } from '@/models/user-type.model';
import { CreateUserTypesInput } from './dto/create-user-types.input';
import { ListUserTypeResponse } from './dto/list-build-sites.response';
import { UpdateUserTypeInput } from './dto/update-build-sites.input';
import { UserType, UserTypeDocument } from '@/models/user-type.model'
@Injectable()
export class UserTypesService {
@@ -11,25 +13,29 @@ export class UserTypesService {
@InjectModel(UserType.name) private readonly userTypesModel: Model<UserTypeDocument>
) { }
async findAll(projection?: any): Promise<UserTypeDocument[]> {
return this.userTypesModel.find({}, projection, { lean: false }).populate({ path: 'person', select: projection?.person }).exec();
async findAll(projection: any, skip: number, limit: number, sort?: Record<string, 1 | -1>, filters?: Record<string, any>): Promise<ListUserTypeResponse> {
const query: any = {}; if (filters && Object.keys(filters).length > 0) { Object.assign(query, filters) };
const totalCount = await this.userTypesModel.countDocuments(query).exec();
const data = await this.userTypesModel.find(query, projection, { lean: true }).skip(skip).limit(limit).sort(sort).exec();
return { data, totalCount };
}
async findById(id: Types.ObjectId, projection?: any): Promise<UserTypeDocument | null> {
return this.userTypesModel.findById(id, projection, { lean: false }).populate({ path: 'person', select: projection?.person }).exec();
return this.userTypesModel.findById(id, projection, { lean: false }).populate({ path: 'buildSites', select: projection?.buildSites }).exec();
}
async create(input: CreateUserTypesInput): Promise<UserTypeDocument> {
const user = new this.userTypesModel(input);
return user.save();
}
async create(input: CreateUserTypesInput): Promise<UserTypeDocument> { const buildSite = new this.userTypesModel(input); return buildSite.save() }
async update(uuid: string, input: UpdateUserTypeInput): Promise<UserTypeDocument> { const buildSite = await this.userTypesModel.findOne({ uuid }); if (!buildSite) { throw new Error('BuildSite not found') }; buildSite.set(input); return buildSite.save() }
async delete(uuid: string): Promise<boolean> { const buildSite = await this.userTypesModel.deleteMany({ uuid }); return buildSite.deletedCount > 0 }
buildProjection(fields: Record<string, any>): any {
const projection: any = {};
for (const key in fields) {
if (key === 'user' && typeof fields[key] === 'object') { for (const subField of Object.keys(fields[key])) { projection[`user.${subField}`] = 1 } }
if (key === 'buildSites' && typeof fields[key] === 'object') { for (const subField of Object.keys(fields[key])) { projection[`buildSites.${subField}`] = 1 } }
else { projection[key] = 1 }
}
return projection;
}; return projection;
}
}