diff --git a/src/industry-change-application/industry-change-application.service.ts b/src/industry-change-application/industry-change-application.service.ts index 0dcdcba..4d1f0c0 100644 --- a/src/industry-change-application/industry-change-application.service.ts +++ b/src/industry-change-application/industry-change-application.service.ts @@ -4,6 +4,7 @@ import { NotFoundException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; +import { instanceToPlain, plainToInstance } from 'class-transformer'; import { Model } from 'mongoose'; import { ApplicationStatus, @@ -27,17 +28,6 @@ const requestedFields = [ 'regulatoryElectionSub', ]; -const fieldsToExpose = [ - 'id', - 'residentSub', - 'current', - 'requested', - 'status', - 'submittedAt', - 'decision', - 'objectStatus', -]; - @Injectable() export class IndustryChangeApplicationService { constructor( @@ -174,12 +164,15 @@ export class IndustryChangeApplicationService { return findApplication.save(); } - // I wrote this because I could not for the life of me get class-transformer to - // play along with mongo documents. I do not have experience with either, so this - // was a last ditch effort. makeReadable(input: IndustryChangeApplication | IndustryChangeApplication[]) { - return Array.isArray(input) - ? input.map((object) => take(takeMongoObject(object), fieldsToExpose)) - : take(takeMongoObject(input), fieldsToExpose); + if (Array.isArray(input)) { + return input.map((item) => this.makeReadable(item)); + } + + const response = plainToInstance( + IndustryChangeApplication, + (input as IndustryChangeApplicationDocument).toJSON(), + ); + return instanceToPlain(response); } } diff --git a/src/industry-change-application/schemas/IndustryChangeApplication.schema.ts b/src/industry-change-application/schemas/IndustryChangeApplication.schema.ts index 0de6301..41d7df9 100644 --- a/src/industry-change-application/schemas/IndustryChangeApplication.schema.ts +++ b/src/industry-change-application/schemas/IndustryChangeApplication.schema.ts @@ -3,6 +3,7 @@ import { HydratedDocument } from 'mongoose'; import { Decision } from './Decision.schema'; import { ApplicationStatus, ObjectStatus } from 'src/enums/status.enum'; import { ICAInformation } from './ICAInformation.schema'; +import { Exclude, Expose, Transform } from 'class-transformer'; export type IndustryChangeApplicationDocument = HydratedDocument; @@ -11,6 +12,13 @@ export type IndustryChangeApplicationDocument = timestamps: true, }) export class IndustryChangeApplication { + @Expose({ name: 'id' }) + @Transform(({ key, obj }) => obj[key].toString()) + _id: string; + + @Exclude() + __v: number; + @Prop({ required: true, }) @@ -42,9 +50,11 @@ export class IndustryChangeApplication { @Prop({ type: Decision }) decision: Decision; + @Exclude() @Prop() createdBy: string; + @Exclude() @Prop() updatedBy: string; @@ -56,8 +66,10 @@ export class IndustryChangeApplication { }) objectStatus: ObjectStatus; + @Exclude() createdAt: Date; + @Exclude() updatedAt: Date; }