updated living space add list updagte
This commit is contained in:
@@ -7,9 +7,6 @@ export class CreateLivingSpaceInput extends ExpiryBaseInput {
|
||||
@Field()
|
||||
buildID: string;
|
||||
|
||||
@Field()
|
||||
collectionToken: string;
|
||||
|
||||
@Field()
|
||||
partID: string;
|
||||
|
||||
|
||||
@@ -4,12 +4,6 @@ import { InputType, Field } from '@nestjs/graphql';
|
||||
@InputType()
|
||||
export class UpdateLivingSpaceInput extends ExpiryBaseInput {
|
||||
|
||||
@Field({ nullable: true })
|
||||
buildID?: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
collectionToken?: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
partID?: string;
|
||||
|
||||
|
||||
@@ -3,9 +3,13 @@ import { LivingSpaceService } from './living-space.service';
|
||||
import { LivingSpaceResolver } from './living-space.resolver';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { LivingSpaces, LivingSpacesSchema } from '@/models/living-spaces.model';
|
||||
import { UserType, UserTypeSchema } from '@/models/user-type.model';
|
||||
|
||||
@Module({
|
||||
imports: [MongooseModule.forFeature([{ name: LivingSpaces.name, schema: LivingSpacesSchema }])],
|
||||
imports: [MongooseModule.forFeature([
|
||||
{ name: LivingSpaces.name, schema: LivingSpacesSchema },
|
||||
{ name: UserType.name, schema: UserTypeSchema },
|
||||
])],
|
||||
providers: [LivingSpaceService, LivingSpaceResolver]
|
||||
})
|
||||
export class LivingSpaceModule { }
|
||||
|
||||
@@ -15,23 +15,23 @@ export class LivingSpaceResolver {
|
||||
constructor(private readonly livingSpaceService: LivingSpaceService) { }
|
||||
|
||||
@Query(() => ListLivingSpaceResponse, { name: 'getLivingSpaces' })
|
||||
async getLivingSpaces(@Info() info: GraphQLResolveInfo, @Args('input') input: ListArguments): Promise<ListLivingSpaceResponse> {
|
||||
async getLivingSpaces(@Info() info: GraphQLResolveInfo, @Args('buildID') buildID: string, @Args('input') input: ListArguments): Promise<ListLivingSpaceResponse> {
|
||||
const fields = graphqlFields(info); const projection = this.livingSpaceService.buildProjection(fields?.data); const { skip, limit, sort, filters } = input;
|
||||
return await this.livingSpaceService.findAll(projection, skip, limit, sort, filters);
|
||||
return await this.livingSpaceService.findAll(buildID, projection, skip, limit, sort, filters);
|
||||
}
|
||||
|
||||
@Query(() => LivingSpaces, { name: 'getLivingSpace', nullable: true })
|
||||
async getLivingSpace(@Args('id', { type: () => ID }) id: string, @Info() info: GraphQLResolveInfo): Promise<LivingSpaces | null> {
|
||||
const fields = graphqlFields(info); const projection = this.livingSpaceService.buildProjection(fields); return this.livingSpaceService.findById(new Types.ObjectId(id), projection);
|
||||
async getLivingSpace(@Args('id', { type: () => ID }) id: string, @Args('buildID') buildID: string, @Info() info: GraphQLResolveInfo): Promise<LivingSpaces | null> {
|
||||
const fields = graphqlFields(info); const projection = this.livingSpaceService.buildProjection(fields); return this.livingSpaceService.findById(buildID, new Types.ObjectId(id), projection);
|
||||
}
|
||||
|
||||
@Mutation(() => LivingSpaces, { name: 'createLivingSpace' })
|
||||
async createLivingSpace(@Args('input') input: CreateLivingSpaceInput): Promise<LivingSpaces> { return this.livingSpaceService.create(input) }
|
||||
|
||||
@Mutation(() => LivingSpaces, { name: 'updateLivingSpace' })
|
||||
async updateLivingSpace(@Args('uuid') uuid: string, @Args('input') input: UpdateLivingSpaceInput): Promise<LivingSpaces> { return this.livingSpaceService.update(uuid, input) }
|
||||
async updateLivingSpace(@Args('buildID') buildID: string, @Args('uuid') uuid: string, @Args('input') input: UpdateLivingSpaceInput): Promise<LivingSpaces> { return this.livingSpaceService.update(buildID, uuid, input) }
|
||||
|
||||
@Mutation(() => Boolean, { name: 'deleteLivingSpace' })
|
||||
async deleteLivingSpace(@Args('uuid') uuid: string, @Args('collectionToken') collectionToken: string): Promise<boolean> { return this.livingSpaceService.delete(uuid, collectionToken) }
|
||||
async deleteLivingSpace(@Args('buildID') buildID: string, @Args('uuid') uuid: string): Promise<boolean> { return this.livingSpaceService.delete(buildID, uuid) }
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Types, Model, Connection } from 'mongoose';
|
||||
import { getDynamicLivingSpaceModel, LivingSpaces, LivingSpacesDocument } from '@/models/living-spaces.model';
|
||||
import { Types, Connection, Model } from 'mongoose';
|
||||
import { getDynamicLivingSpaceModel, LivingSpacesDocument } from '@/models/living-spaces.model';
|
||||
import { ListLivingSpaceResponse } from './dto/list-living-space.response';
|
||||
import { CreateLivingSpaceInput } from './dto/create-living-space.input';
|
||||
import { UpdateLivingSpaceInput } from './dto/update-living-space.input';
|
||||
import { InjectConnection } from '@nestjs/mongoose';
|
||||
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
|
||||
import { UserTypeDocument } from '@/models/user-type.model';
|
||||
|
||||
interface UpdateInput {
|
||||
build?: Types.ObjectId;
|
||||
collectionToken?: string;
|
||||
userType?: Types.ObjectId;
|
||||
part?: Types.ObjectId;
|
||||
part?: Types.ObjectId | null;
|
||||
company?: Types.ObjectId;
|
||||
person?: Types.ObjectId;
|
||||
expiryStarts?: Date;
|
||||
@@ -22,27 +20,26 @@ interface UpdateInput {
|
||||
export class LivingSpaceService {
|
||||
|
||||
constructor(
|
||||
@InjectModel(LivingSpaces.name) private readonly livingSpaceModel: Model<LivingSpacesDocument>,
|
||||
@InjectConnection() private readonly connection: Connection
|
||||
@InjectConnection() private readonly connection: Connection,
|
||||
@InjectModel('UserType') private readonly userTypeModel: Model<UserTypeDocument>
|
||||
) { }
|
||||
|
||||
async findAll(projection: any, skip: number, limit: number, sort?: Record<string, 1 | -1>, filters?: Record<string, any>): Promise<ListLivingSpaceResponse> {
|
||||
if (!filters?.collectionToken) { throw new Error('Collection token is required') }
|
||||
async findAll(buildID: string, projection: any, skip: number, limit: number, sort?: Record<string, 1 | -1>, filters?: Record<string, any>): Promise<ListLivingSpaceResponse> {
|
||||
const selectedModel = getDynamicLivingSpaceModel(buildID, this.connection);
|
||||
const query: any = {}; if (filters && Object.keys(filters).length > 0) { Object.assign(query, filters) };
|
||||
const totalCount = await this.livingSpaceModel.countDocuments(query).exec();
|
||||
const data = await this.livingSpaceModel.find(query, projection, { lean: true }).skip(skip).limit(limit).sort(sort).exec();
|
||||
const totalCount = await selectedModel.countDocuments(query).exec();
|
||||
const data = await selectedModel.find(query, projection, { lean: true }).skip(skip).limit(limit).sort(sort).exec();
|
||||
return { data, totalCount };
|
||||
}
|
||||
|
||||
async findById(id: Types.ObjectId, projection?: any): Promise<LivingSpacesDocument | null> {
|
||||
if (!projection?.collectionToken) { throw new Error('Collection token is required') }
|
||||
const selectedModel = getDynamicLivingSpaceModel(projection.collectionToken, this.connection);
|
||||
async findById(buildID: string, id: Types.ObjectId, projection?: any): Promise<LivingSpacesDocument | null> {
|
||||
const selectedModel = getDynamicLivingSpaceModel(buildID, this.connection);
|
||||
return selectedModel.findById(id, projection, { lean: false }).populate({ path: 'company', select: projection?.company }).exec();
|
||||
}
|
||||
|
||||
async create(input: CreateLivingSpaceInput): Promise<LivingSpacesDocument> {
|
||||
if (!input.collectionToken) { throw new Error('Collection token is required') }
|
||||
const LivingSpaceModel = getDynamicLivingSpaceModel(input.collectionToken, this.connection);
|
||||
if (!input.buildID) { throw new Error('Build ID is required') }
|
||||
const LivingSpaceModel = getDynamicLivingSpaceModel(input.buildID, this.connection);
|
||||
const docInput: Partial<LivingSpacesDocument> = {
|
||||
build: new Types.ObjectId(input.buildID), part: new Types.ObjectId(input.partID), userType: new Types.ObjectId(input.userTypeID),
|
||||
company: new Types.ObjectId(input.companyID), person: new Types.ObjectId(input.personID),
|
||||
@@ -50,18 +47,18 @@ export class LivingSpaceService {
|
||||
}; const doc = new LivingSpaceModel(docInput); return await doc.save()
|
||||
}
|
||||
|
||||
async update(uuid: string, input: UpdateLivingSpaceInput): Promise<LivingSpacesDocument> {
|
||||
if (!input.collectionToken) { throw new Error('Collection token is required') }
|
||||
const selectedModel = getDynamicLivingSpaceModel(input.collectionToken, this.connection);
|
||||
const newInput: UpdateInput = {}
|
||||
if (input?.buildID) { newInput.build = new Types.ObjectId(input.buildID) }; if (input?.userTypeID) { newInput.userType = new Types.ObjectId(input.userTypeID) }
|
||||
if (input?.partID) { newInput.part = new Types.ObjectId(input.partID) }; if (input?.companyID) { newInput.company = new Types.ObjectId(input.companyID) }
|
||||
if (input?.personID) { newInput.person = new Types.ObjectId(input.personID) };
|
||||
async update(buildID: string, uuid: string, input: UpdateLivingSpaceInput): Promise<LivingSpacesDocument> {
|
||||
if (!buildID) { throw new Error('Build ID is required') }
|
||||
const selectedModel = getDynamicLivingSpaceModel(buildID, this.connection);
|
||||
const livingSpace = await selectedModel.findOne({ uuid }); const userTypeSelected = await this.userTypeModel.findById(new Types.ObjectId(input.userTypeID)).exec(); const newInput: UpdateInput = {}
|
||||
if (userTypeSelected?.isProperty) { if (input?.partID) { newInput.part = new Types.ObjectId(input.partID) } } else { newInput.part = null }
|
||||
if (input?.companyID) { newInput.company = new Types.ObjectId(input.companyID) }
|
||||
if (input?.personID) { newInput.person = new Types.ObjectId(input.personID) }; if (input?.userTypeID) { newInput.userType = new Types.ObjectId(input.userTypeID) }
|
||||
if (input?.expiryStarts) { newInput.expiryStarts = input.expiryStarts }; if (input?.expiryEnds) { newInput.expiryEnds = input.expiryEnds }
|
||||
const company = await selectedModel.findOne({ uuid }); if (!company) { throw new Error('Company not found') }; company.set(newInput); return company.save();
|
||||
if (!livingSpace) { throw new Error('Company not found') }; livingSpace.set(newInput); return livingSpace.save();
|
||||
}
|
||||
|
||||
async delete(uuid: string, collectionToken: string): Promise<boolean> { if (!collectionToken) { throw new Error('Collection token is required') } const selectedModel = getDynamicLivingSpaceModel(collectionToken, this.connection); const company = await selectedModel.deleteMany({ uuid }); return company.deletedCount > 0 }
|
||||
async delete(buildID: string, uuid: string): Promise<boolean> { if (!buildID) { throw new Error('Build ID is required') } const selectedModel = getDynamicLivingSpaceModel(buildID, this.connection); const company = await selectedModel.deleteMany({ uuid }); return company.deletedCount > 0 }
|
||||
|
||||
buildProjection(fields: Record<string, any>): any {
|
||||
const projection: any = {};
|
||||
|
||||
@@ -20,9 +20,9 @@ export class LivingSpaces extends Base {
|
||||
@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, { nullable: true })
|
||||
@Prop({ type: Types.ObjectId, ref: BuildParts.name, required: false })
|
||||
part?: Types.ObjectId | null;
|
||||
|
||||
@Field(() => ID)
|
||||
@Prop({ type: Types.ObjectId, ref: UserType.name, required: true })
|
||||
@@ -41,8 +41,8 @@ export class LivingSpaces extends Base {
|
||||
export type LivingSpacesDocument = LivingSpaces & Document;
|
||||
export const LivingSpacesSchema = SchemaFactory.createForClass(LivingSpaces);
|
||||
|
||||
export function getDynamicLivingSpaceModel(collectionToken: string, connection: Connection): Model<LivingSpacesDocument> {
|
||||
const collectionName = `LivingSpaces${collectionToken}`;
|
||||
export function getDynamicLivingSpaceModel(buildID: string, connection: Connection): Model<LivingSpacesDocument> {
|
||||
const collectionName = `LivingSpaces${buildID}`;
|
||||
const schema = SchemaFactory.createForClass(LivingSpaces);
|
||||
if (connection.models[collectionName]) { return connection.models[collectionName] as Model<LivingSpacesDocument> }
|
||||
return connection.model(collectionName, schema, collectionName) as unknown as Model<LivingSpacesDocument>;
|
||||
|
||||
Reference in New Issue
Block a user