parts and areas tested
This commit is contained in:
@@ -3,9 +3,12 @@ import { BuildPartsService } from './build-parts.service';
|
||||
import { BuildPartsResolver } from './build-parts.resolver';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { BuildParts, BuildPartsSchema } from '@/models/build-parts.model';
|
||||
import { Build, BuildSchema } from '@/models/build.model';
|
||||
|
||||
@Module({
|
||||
imports: [MongooseModule.forFeature([{ name: BuildParts.name, schema: BuildPartsSchema }])],
|
||||
imports: [MongooseModule.forFeature([
|
||||
{ name: BuildParts.name, schema: BuildPartsSchema },
|
||||
{ name: Build.name, schema: BuildSchema }])],
|
||||
providers: [BuildPartsService, BuildPartsResolver]
|
||||
})
|
||||
export class BuildPartsModule { }
|
||||
@@ -2,27 +2,36 @@ import { Resolver, Query, Args, ID, Info, Mutation } from '@nestjs/graphql';
|
||||
import { Types } from 'mongoose';
|
||||
import { BuildParts } from '@/models/build-parts.model';
|
||||
import { CreateBuildPartsInput } from './dto/create-build-part.input';
|
||||
import { BuildPartsService } from './build-parts.service';
|
||||
import { ListArguments } from '@/dto/list.input';
|
||||
import { ListBuildPartsResponse } from './dto/list-build-parts.response';
|
||||
import graphqlFields from 'graphql-fields';
|
||||
import type { GraphQLResolveInfo } from 'graphql';
|
||||
import { BuildPartsService } from './build-parts.service';
|
||||
import { UpdateBuildPartsInput } from './dto/update-build-parts.input';
|
||||
|
||||
@Resolver()
|
||||
export class BuildPartsResolver {
|
||||
|
||||
constructor(private readonly buildPartsService: BuildPartsService) { }
|
||||
|
||||
@Query(() => [BuildParts], { name: 'BuildParts' })
|
||||
async getBuildParts(@Info() info: GraphQLResolveInfo): Promise<BuildParts[]> {
|
||||
const fields = graphqlFields(info); const projection = this.buildPartsService.buildProjection(fields); return this.buildPartsService.findAll(projection);
|
||||
@Query(() => ListBuildPartsResponse, { name: 'buildParts' })
|
||||
async getBuildParts(@Info() info: GraphQLResolveInfo, @Args('input') input: ListArguments): Promise<ListBuildPartsResponse> {
|
||||
const fields = graphqlFields(info); const projection = this.buildPartsService.buildProjection(fields?.data); const { skip, limit, sort, filters } = input;
|
||||
return await this.buildPartsService.findAll(projection, skip, limit, sort, filters);
|
||||
}
|
||||
|
||||
@Query(() => BuildParts, { name: 'BuildParts', nullable: true })
|
||||
@Query(() => BuildParts, { name: 'buildPart', nullable: true })
|
||||
async getBuildPart(@Args('id', { type: () => ID }) id: string, @Info() info: GraphQLResolveInfo): Promise<BuildParts | null> {
|
||||
const fields = graphqlFields(info); const projection = this.buildPartsService.buildProjection(fields); return this.buildPartsService.findById(new Types.ObjectId(id), projection);
|
||||
}
|
||||
|
||||
@Mutation(() => BuildParts, { name: 'createBuildPart' })
|
||||
async createBuildPart(@Args('input') input: CreateBuildPartsInput): Promise<BuildParts> {
|
||||
return this.buildPartsService.create(input);
|
||||
}
|
||||
async createBuildPart(@Args('input') input: CreateBuildPartsInput): Promise<BuildParts> { return this.buildPartsService.create(input) }
|
||||
|
||||
@Mutation(() => Boolean, { name: 'deleteBuildPart' })
|
||||
async deleteBuildPart(@Args('uuid') uuid: string): Promise<boolean> { return this.buildPartsService.delete(uuid) }
|
||||
|
||||
@Mutation(() => BuildParts, { name: 'updateBuildPart' })
|
||||
async updateBuildPart(@Args('uuid') uuid: string, @Args('input') input: UpdateBuildPartsInput): Promise<BuildParts> { return this.buildPartsService.update(uuid, input) }
|
||||
|
||||
}
|
||||
|
||||
@@ -3,14 +3,24 @@ import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Types, Model } from 'mongoose';
|
||||
import { BuildParts, BuildPartsDocument } from '@/models/build-parts.model';
|
||||
import { CreateBuildPartsInput } from './dto/create-build-part.input';
|
||||
import { ListBuildPartsResponse } from './dto/list-build-parts.response';
|
||||
import { UpdateBuildPartsInput } from './dto/update-build-parts.input';
|
||||
import { Build, BuildDocument } from '@/models/build.model';
|
||||
|
||||
@Injectable()
|
||||
export class BuildPartsService {
|
||||
|
||||
constructor(@InjectModel(BuildParts.name) private readonly buildPartsModel: Model<BuildPartsDocument>) { }
|
||||
constructor(
|
||||
@InjectModel(BuildParts.name) private readonly buildPartsModel: Model<BuildPartsDocument>,
|
||||
@InjectModel(Build.name) private readonly buildModel: Model<BuildDocument>
|
||||
) { }
|
||||
|
||||
async findAll(projection?: any): Promise<BuildPartsDocument[]> {
|
||||
return this.buildPartsModel.find({}, projection, { lean: false }).populate({ path: 'buildParts', select: projection?.buildParts }).exec();
|
||||
async findAll(projection: any, skip: number, limit: number, sort?: Record<string, 1 | -1>, filters?: Record<string, any>): Promise<ListBuildPartsResponse> {
|
||||
const query: any = {}; if (filters && Object.keys(filters).length > 0) { Object.assign(query, filters) };
|
||||
if (query?.buildId) { query.buildId = new Types.ObjectId(query?.buildId) };
|
||||
const totalCount = await this.buildPartsModel.countDocuments(query).exec();
|
||||
const data = await this.buildPartsModel.find(query, projection, { lean: true }).skip(skip).limit(limit).sort(sort).exec();
|
||||
return { data, totalCount };
|
||||
}
|
||||
|
||||
async findById(id: Types.ObjectId, projection?: any): Promise<BuildPartsDocument | null> {
|
||||
@@ -18,10 +28,22 @@ export class BuildPartsService {
|
||||
}
|
||||
|
||||
async create(input: CreateBuildPartsInput): Promise<BuildPartsDocument> {
|
||||
const buildParts = new this.buildPartsModel(input);
|
||||
return buildParts.save();
|
||||
const getBuildID = await this.buildModel.findById(input?.buildId, { _id: 1 }, { lean: true }).exec();
|
||||
if (getBuildID) { input.buildId = new Types.ObjectId(getBuildID._id) };
|
||||
if (input?.directionId) { input.directionId = new Types.ObjectId(input?.directionId) };
|
||||
if (input?.typeId) { input.typeId = new Types.ObjectId(input?.typeId) };
|
||||
const buildParts = new this.buildPartsModel(input); return buildParts.save()
|
||||
}
|
||||
|
||||
async update(uuid: string, input: UpdateBuildPartsInput): Promise<BuildPartsDocument> {
|
||||
const buildParts = await this.buildPartsModel.findOne({ _id: new Types.ObjectId(uuid) }); if (!buildParts) { throw new Error('BuildParts not found') };
|
||||
if (input?.buildId) { const getBuildID = await this.buildModel.findById(input?.buildId, { _id: 1 }, { lean: true }).exec(); if (getBuildID) { input.buildId = new Types.ObjectId(getBuildID._id) } };
|
||||
if (input?.directionId) { input.directionId = new Types.ObjectId(input?.directionId) };
|
||||
if (input?.typeId) { input.typeId = new Types.ObjectId(input?.typeId) }; buildParts.set(input); return buildParts.save()
|
||||
}
|
||||
|
||||
async delete(uuid: string): Promise<boolean> { console.log(uuid); const buildParts = await this.buildPartsModel.deleteMany({ _id: new Types.ObjectId(uuid) }); return buildParts.deletedCount > 0 }
|
||||
|
||||
buildProjection(fields: Record<string, any>): any {
|
||||
const projection: any = {};
|
||||
for (const key in fields) {
|
||||
|
||||
@@ -1,10 +1,55 @@
|
||||
import { InputType, Field, ID } from '@nestjs/graphql';
|
||||
|
||||
import { InputType, Field, ID, Float } from '@nestjs/graphql';
|
||||
import { Types } from 'mongoose';
|
||||
|
||||
@InputType()
|
||||
export class CreateBuildPartsInput {
|
||||
|
||||
@Field(() => ID)
|
||||
build: string;
|
||||
@Field(() => ID, { nullable: false })
|
||||
buildId: Types.ObjectId;
|
||||
|
||||
@Field({ nullable: false })
|
||||
addressGovCode: string;
|
||||
|
||||
@Field(() => Float, { nullable: false })
|
||||
no: number;
|
||||
|
||||
@Field(() => Float, { nullable: false })
|
||||
level: number;
|
||||
|
||||
@Field({ nullable: false })
|
||||
code: string;
|
||||
|
||||
@Field(() => Float, { nullable: false })
|
||||
grossSize: number;
|
||||
|
||||
@Field(() => Float, { nullable: false })
|
||||
netSize: number;
|
||||
|
||||
@Field({ nullable: false })
|
||||
defaultAccessory: string;
|
||||
|
||||
@Field({ nullable: false })
|
||||
humanLivability: boolean;
|
||||
|
||||
@Field({ nullable: false })
|
||||
key: string;
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
directionId: Types.ObjectId;
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
typeId: Types.ObjectId;
|
||||
|
||||
@Field(() => Boolean, { nullable: false })
|
||||
active: boolean;
|
||||
|
||||
@Field(() => Boolean, { nullable: false })
|
||||
isConfirmed: boolean;
|
||||
|
||||
@Field(() => String, { nullable: true })
|
||||
expiryStarts: string;
|
||||
|
||||
@Field(() => String, { nullable: true })
|
||||
expiryEnds: string;
|
||||
|
||||
}
|
||||
|
||||
13
backend/src/build-parts/dto/list-build-parts.response.ts
Normal file
13
backend/src/build-parts/dto/list-build-parts.response.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { BuildParts } from "@/models/build-parts.model";
|
||||
import { Field, Int, ObjectType } from "@nestjs/graphql";
|
||||
|
||||
@ObjectType()
|
||||
export class ListBuildPartsResponse {
|
||||
|
||||
@Field(() => [BuildParts], { nullable: true })
|
||||
data?: BuildParts[];
|
||||
|
||||
@Field(() => Int, { nullable: true })
|
||||
totalCount?: number;
|
||||
|
||||
}
|
||||
50
backend/src/build-parts/dto/update-build-parts.input.ts
Normal file
50
backend/src/build-parts/dto/update-build-parts.input.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { ExpiryBaseInput } from "@/models/base.model";
|
||||
import { InputType, Field, ID, Float } from "@nestjs/graphql";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
@InputType()
|
||||
export class UpdateBuildPartsInput extends ExpiryBaseInput {
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
buildId?: Types.ObjectId;
|
||||
|
||||
@Field({ nullable: true })
|
||||
addressGovCode?: string;
|
||||
|
||||
@Field(() => Float, { nullable: true })
|
||||
no?: number;
|
||||
|
||||
@Field(() => Float, { nullable: true })
|
||||
level?: number;
|
||||
|
||||
@Field({ nullable: true })
|
||||
code?: string;
|
||||
|
||||
@Field(() => Float, { nullable: true })
|
||||
grossSize?: number;
|
||||
|
||||
@Field(() => Float, { nullable: true })
|
||||
netSize?: number;
|
||||
|
||||
@Field({ nullable: true })
|
||||
defaultAccessory?: string;
|
||||
|
||||
@Field({ nullable: true })
|
||||
humanLivability?: boolean;
|
||||
|
||||
@Field({ nullable: true })
|
||||
key?: string;
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
directionId?: Types.ObjectId;
|
||||
|
||||
@Field(() => ID, { nullable: true })
|
||||
typeId?: Types.ObjectId;
|
||||
|
||||
@Field(() => Boolean, { nullable: true })
|
||||
active?: boolean;
|
||||
|
||||
@Field(() => Boolean, { nullable: true })
|
||||
isConfirmed?: boolean;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user