Compare commits

..

1 Commits

Author SHA1 Message Date
8c761c66a8
fix mongo class transform 2022-12-08 20:08:31 +02:00
2 changed files with 22 additions and 17 deletions

View File

@ -4,6 +4,7 @@ import {
NotFoundException, NotFoundException,
} from '@nestjs/common'; } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose';
import { instanceToPlain, plainToInstance } from 'class-transformer';
import { Model } from 'mongoose'; import { Model } from 'mongoose';
import { import {
ApplicationStatus, ApplicationStatus,
@ -27,17 +28,6 @@ const requestedFields = [
'regulatoryElectionSub', 'regulatoryElectionSub',
]; ];
const fieldsToExpose = [
'id',
'residentSub',
'current',
'requested',
'status',
'submittedAt',
'decision',
'objectStatus',
];
@Injectable() @Injectable()
export class IndustryChangeApplicationService { export class IndustryChangeApplicationService {
constructor( constructor(
@ -174,12 +164,15 @@ export class IndustryChangeApplicationService {
return findApplication.save(); 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[]) { makeReadable(input: IndustryChangeApplication | IndustryChangeApplication[]) {
return Array.isArray(input) if (Array.isArray(input)) {
? input.map((object) => take(takeMongoObject(object), fieldsToExpose)) return input.map((item) => this.makeReadable(item));
: take(takeMongoObject(input), fieldsToExpose); }
const response = plainToInstance(
IndustryChangeApplication,
(input as IndustryChangeApplicationDocument).toJSON(),
);
return instanceToPlain(response);
} }
} }

View File

@ -3,6 +3,7 @@ import { HydratedDocument } from 'mongoose';
import { Decision } from './Decision.schema'; import { Decision } from './Decision.schema';
import { ApplicationStatus, ObjectStatus } from 'src/enums/status.enum'; import { ApplicationStatus, ObjectStatus } from 'src/enums/status.enum';
import { ICAInformation } from './ICAInformation.schema'; import { ICAInformation } from './ICAInformation.schema';
import { Exclude, Expose, Transform } from 'class-transformer';
export type IndustryChangeApplicationDocument = export type IndustryChangeApplicationDocument =
HydratedDocument<IndustryChangeApplication>; HydratedDocument<IndustryChangeApplication>;
@ -11,6 +12,13 @@ export type IndustryChangeApplicationDocument =
timestamps: true, timestamps: true,
}) })
export class IndustryChangeApplication { export class IndustryChangeApplication {
@Expose({ name: 'id' })
@Transform(({ key, obj }) => obj[key].toString())
_id: string;
@Exclude()
__v: number;
@Prop({ @Prop({
required: true, required: true,
}) })
@ -42,9 +50,11 @@ export class IndustryChangeApplication {
@Prop({ type: Decision }) @Prop({ type: Decision })
decision: Decision; decision: Decision;
@Exclude()
@Prop() @Prop()
createdBy: string; createdBy: string;
@Exclude()
@Prop() @Prop()
updatedBy: string; updatedBy: string;
@ -56,8 +66,10 @@ export class IndustryChangeApplication {
}) })
objectStatus: ObjectStatus; objectStatus: ObjectStatus;
@Exclude()
createdAt: Date; createdAt: Date;
@Exclude()
updatedAt: Date; updatedAt: Date;
} }