json-api-nestjs
Version:
JsonApi Plugin for NestJs
40 lines • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.patchOne = patchOne;
const common_1 = require("@nestjs/common");
const nestjs_shared_1 = require("../../../../utils/nestjs-shared");
async function patchOne(id, inputData) {
const { id: idBody, attributes, relationships } = inputData;
if (`${id}` !== idBody) {
const error = {
code: 'invalid_arguments',
message: `Data 'id' must be equal to url param`,
path: ['data', 'id'],
};
throw new common_1.UnprocessableEntityException([error]);
}
const existEntity = await this.microOrmUtilService
.queryBuilder()
.where({
[this.microOrmUtilService.currentPrimaryColumn]: id,
})
.getSingleResult();
if (!existEntity) {
const error = {
code: 'invalid_arguments',
message: `Resource '${this.microOrmUtilService.currentAlias}' with id '${id}' does not exist`,
path: ['data', 'id'],
};
throw new common_1.NotFoundException([error]);
}
if (attributes) {
const attrTarget = this.microOrmUtilService.createEntity(attributes);
for (const [props, val] of nestjs_shared_1.ObjectTyped.entries(attrTarget)) {
if (!(props in attributes))
continue;
existEntity[props] = val;
}
}
return this.microOrmUtilService.saveEntity(existEntity, relationships);
}
//# sourceMappingURL=patch-one.js.map