fix customer put queries
This commit is contained in:
parent
78c7364a81
commit
e72d4e9203
@ -1,9 +1,9 @@
|
||||
import { Response, Request } from "express";
|
||||
import { Controller, Delete, Get, Post, Put } from "@ControllerPattern/index";
|
||||
import { Controller, Delete, Get, Post } from "@ControllerPattern/index";
|
||||
import ApiController from "@Common/system/controller-pattern/ApiController";
|
||||
import { Service } from "typedi";
|
||||
import FilesService from "@Services/common/FilesService/FilesService";
|
||||
import { Files, Prisma } from "@prisma/client";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { File } from "le-coffre-resources/dist/Customer";
|
||||
import { Document } from "le-coffre-resources/dist/Customer";
|
||||
import DocumentsService from "@Services/customer/DocumentsService/DocumentsService";
|
||||
@ -15,7 +15,11 @@ import { validateOrReject } from "class-validator";
|
||||
@Controller()
|
||||
@Service()
|
||||
export default class FilesController extends ApiController {
|
||||
constructor(private filesService: FilesService, private documentService: DocumentsService, private notificationBuilder : NotificationBuilder) {
|
||||
constructor(
|
||||
private filesService: FilesService,
|
||||
private documentService: DocumentsService,
|
||||
private notificationBuilder: NotificationBuilder,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
@ -37,7 +41,7 @@ export default class FilesController extends ApiController {
|
||||
return;
|
||||
}
|
||||
if (query.where?.document?.depositor) delete query.where.document.depositor;
|
||||
const customerWhereInput: Prisma.FilesWhereInput = { ...query.where, document: {depositor: { contact: { email: email } } }};
|
||||
const customerWhereInput: Prisma.FilesWhereInput = { ...query.where, document: { depositor: { contact: { email: email } } } };
|
||||
query.where = customerWhereInput;
|
||||
if (query.include?.document) delete query.include.document;
|
||||
|
||||
@ -120,7 +124,6 @@ export default class FilesController extends ApiController {
|
||||
strategy: "excludeAll",
|
||||
});
|
||||
|
||||
|
||||
//success
|
||||
this.httpCreated(response, fileEntityHydrated);
|
||||
} catch (error) {
|
||||
@ -129,41 +132,6 @@ export default class FilesController extends ApiController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Update a specific file
|
||||
*/
|
||||
@Put("/api/v1/customer/files/:uid", [authHandler, fileHandler])
|
||||
protected async update(req: Request, response: Response) {
|
||||
try {
|
||||
const uid = req.params["uid"];
|
||||
if (!uid) {
|
||||
throw new Error("No uid provided");
|
||||
}
|
||||
|
||||
const fileFound = await this.filesService.getByUid(uid);
|
||||
|
||||
if (!fileFound) {
|
||||
this.httpNotFoundRequest(response, "file not found");
|
||||
return;
|
||||
}
|
||||
|
||||
//init File resource with request body values
|
||||
const fileEntity = File.hydrate<File>(req.body);
|
||||
|
||||
//call service to get prisma entity
|
||||
const fileEntityUpdated: Files = await this.filesService.update(uid, fileEntity);
|
||||
|
||||
//Hydrate ressource with prisma entity
|
||||
const file = File.hydrate<File>(fileEntityUpdated, { strategy: "excludeAll" });
|
||||
|
||||
//success
|
||||
this.httpSuccess(response, file);
|
||||
} catch (error) {
|
||||
this.httpBadRequest(response, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Delete a specific File
|
||||
*/
|
||||
@ -217,7 +185,7 @@ export default class FilesController extends ApiController {
|
||||
let query;
|
||||
if (req.query["q"]) {
|
||||
query = JSON.parse(req.query["q"] as string);
|
||||
if(query.document) delete query.document;
|
||||
if (query.document) delete query.document;
|
||||
}
|
||||
|
||||
const fileEntity = await this.filesService.getByUid(uid, query);
|
||||
|
@ -125,27 +125,29 @@ export default class CustomersController extends ApiController {
|
||||
return;
|
||||
}
|
||||
|
||||
const customers = await this.customersService.get({
|
||||
where: {
|
||||
contact: { email: customerEntity.contact?.email },
|
||||
office_folders: {
|
||||
some: {
|
||||
office_uid: req.body.user.office_Id,
|
||||
if (customerEntity.contact?.email) {
|
||||
const customers = await this.customersService.get({
|
||||
where: {
|
||||
contact: { email: customerEntity.contact?.email },
|
||||
office_folders: {
|
||||
some: {
|
||||
office_uid: req.body.user.office_Id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
if (customers.length != 0) {
|
||||
try {
|
||||
customers.forEach((customer) => {
|
||||
if (customer.uid != uid) {
|
||||
throw new Error("email déjà utilisé");
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
this.httpValidationError(response, [{ property: "email", constraints: { unique: "email déjà utilisé" } }]);
|
||||
return;
|
||||
if (customers.length != 0) {
|
||||
try {
|
||||
customers.forEach((customer) => {
|
||||
if (customer.uid != uid) {
|
||||
throw new Error("email déjà utilisé");
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
this.httpValidationError(response, [{ property: "email", constraints: { unique: "email déjà utilisé" } }]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user