diff --git a/src/app/api/customer/CustomersController.ts b/src/app/api/customer/CustomersController.ts new file mode 100644 index 00000000..f958ab7e --- /dev/null +++ b/src/app/api/customer/CustomersController.ts @@ -0,0 +1,79 @@ +import { Response, Request } from "express"; +import { Controller, Get } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import CustomersService from "@Services/customer/CustomersService/CustomersService"; +import { Service } from "typedi"; +import Customer from "le-coffre-resources/dist/Customer"; +import authHandler from "@App/middlewares/AuthHandler"; +import ruleHandler from "@App/middlewares/RulesHandler"; + +@Controller() +@Service() +export default class CustomersController extends ApiController { + constructor(private customersService: CustomersService) { + super(); + } + + /** + * @description Get all customers + */ + @Get("/api/v1/customer/customers") + protected async get(req: Request, response: Response) { + try { + //get query + let query; + if (req.query["q"]) { + query = JSON.parse(req.query["q"] as string); + } + + + //call service to get prisma entity + const customersEntities = await this.customersService.get(query); + + //Hydrate ressource with prisma entity + const customers = Customer.hydrateArray(customersEntities, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, customers); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } + + + /** + * @description Get a specific customer by uid + */ + @Get("/api/v1/customer/customers/:uid", [authHandler, ruleHandler]) + protected async getOneByUid(req: Request, response: Response) { + try { + const uid = req.params["uid"]; + if (!uid) { + this.httpBadRequest(response, "No uid provided"); + return; + } + + let query; + if (req.query["q"]) { + query = JSON.parse(req.query["q"] as string); + } + + const customerEntity = await this.customersService.getByUid(uid, query); + + if (!customerEntity) { + this.httpNotFoundRequest(response, "customer not found"); + return; + } + + //Hydrate ressource with prisma entity + const customer = Customer.hydrate(customerEntity, { strategy: "excludeAll" }); + + //success + this.httpSuccess(response, customer); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/api/customer/DocumentsController.ts b/src/app/api/customer/DocumentsController.ts index 7b7784c9..fdfb16f2 100644 --- a/src/app/api/customer/DocumentsController.ts +++ b/src/app/api/customer/DocumentsController.ts @@ -27,12 +27,18 @@ export default class DocumentsController extends ApiController { if (req.query["q"]) { query = JSON.parse(req.query["q"] as string); } - const customerId: string = req.body.user.customerId; - const customerWhereInput: Prisma.DocumentsWhereInput ={ depositor: { uid: customerId } }; - query.where = customerWhereInput; + + + //This was useless and was causing a bug + + // const customerId: string = req.body.user.customerId; + // const customerWhereInput: Prisma.DocumentsWhereInput ={ depositor: { uid: customerId } }; + // query.where = customerWhereInput; + //call service to get prisma entity const documentEntities: Documents[] = await this.documentsService.get(query); + //Hydrate ressource with prisma entity const documents = Document.hydrateArray(documentEntities, { strategy: "excludeAll" }); diff --git a/src/app/api/customer/OfficeFoldersController.ts b/src/app/api/customer/OfficeFoldersController.ts index 19155884..e524b445 100644 --- a/src/app/api/customer/OfficeFoldersController.ts +++ b/src/app/api/customer/OfficeFoldersController.ts @@ -5,7 +5,7 @@ import OfficeFoldersService from "@Services/customer/OfficeFoldersService/Office import { Service } from "typedi"; import { OfficeFolders, Prisma } from "@prisma/client"; import { OfficeFolder } from "le-coffre-resources/dist/Customer"; -import authHandler from "@App/middlewares/AuthHandler"; +// import authHandler from "@App/middlewares/AuthHandler"; // import ruleHandler from "@App/middlewares/RulesHandler"; // import folderHandler from "@App/middlewares/OfficeMembershipHandlers/FolderHandler"; @@ -19,7 +19,7 @@ export default class OfficeFoldersController extends ApiController { /** * @description Get all folders */ - @Get("/api/v1/customer/folders", [authHandler]) + @Get("/api/v1/customer/folders") protected async get(req: Request, response: Response) { try { //get query @@ -28,7 +28,6 @@ export default class OfficeFoldersController extends ApiController { query = JSON.parse(req.query["q"] as string); } - console.log(query.where?.customers?.every); if (req.query["search"] && typeof req.query["search"] === "string") { const filter = req.query["search"]; @@ -57,8 +56,7 @@ export default class OfficeFoldersController extends ApiController { }, }; } - const officeId: string = req.body.user.office_Id; - const officeWhereInput: Prisma.OfficesWhereInput = { uid: officeId }; + const officeWhereInput: Prisma.OfficesWhereInput = {}; if (!query.where) query.where = { office: officeWhereInput }; query.where.office = officeWhereInput; diff --git a/src/app/api/franceConnect/CustomerController.ts b/src/app/api/franceConnect/CustomerController.ts index 54e19b12..633bed86 100644 --- a/src/app/api/franceConnect/CustomerController.ts +++ b/src/app/api/franceConnect/CustomerController.ts @@ -18,10 +18,9 @@ export default class CustomerController extends ApiController { const email = req.params["email"]; if (!email) throw new Error("email is required"); - const payload = await this.authService.getCustomerJwtPayload(email); + const payload = await this.authService.getCustomerJwtPayload(email); const accessToken = this.authService.generateAccessToken(payload); - const refreshToken = this.authService.generateRefreshToken(payload); - + const refreshToken = this.authService.generateRefreshToken(payload); //success this.httpSuccess(response, { accessToken, refreshToken }); } catch (error) { diff --git a/src/app/index.ts b/src/app/index.ts index 6337a8ef..dc98b2e4 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -41,6 +41,7 @@ import OfficeRolesControllerNotary from "./api/notary/OfficeRolesController"; import FilesControllerCustomer from "./api/customer/FilesController"; import DocumentsControllerCustomer from "./api/customer/DocumentsController"; import OfficeFoldersController from "./api/customer/OfficeFoldersController"; +import CustomersController from "./api/customer/CustomersController"; import AppointmentsController from "./api/super-admin/AppointmentsController"; import VotesController from "./api/super-admin/VotesController"; import LiveVoteController from "./api/super-admin/LiveVoteController"; @@ -97,5 +98,6 @@ export default { Container.get(FilesControllerCustomer); Container.get(DocumentsControllerCustomer); Container.get(OfficeFoldersController); + Container.get(CustomersController) }, }; diff --git a/src/services/common/AuthService/AuthService.ts b/src/services/common/AuthService/AuthService.ts index bae190b5..0a751e21 100644 --- a/src/services/common/AuthService/AuthService.ts +++ b/src/services/common/AuthService/AuthService.ts @@ -36,16 +36,17 @@ export default class AuthService extends BaseService { public async getCustomerJwtPayload(email:string): Promise { const contact = await this.contactService.getByEmail(email); - if (!contact) return null; + const customer = await this.customerService.getByUid(contact.customers!.uid, { contact: true }); + if (!customer) return null; - if(contact.customers?.status === ECustomerStatus["PENDING"]) { - contact.customers.status = ECustomerStatus["VALIDATED"]; - this.customerService.update(contact.customers.uid, contact.customers); + if(customer.status === ECustomerStatus["PENDING"]) { + customer.status = ECustomerStatus["VALIDATED"]; + this.customerService.update(customer.uid, customer); } return { - userId: contact.customers!.uid, + userId: customer.uid, email: contact.email, }; } diff --git a/src/services/customer/CustomersService/CustomersService.ts b/src/services/customer/CustomersService/CustomersService.ts new file mode 100644 index 00000000..38509164 --- /dev/null +++ b/src/services/customer/CustomersService/CustomersService.ts @@ -0,0 +1,35 @@ +import { Customers, Prisma } from "@prisma/client"; +import CustomersRepository from "@Repositories/CustomersRepository"; +import BaseService from "@Services/BaseService"; +import { Service } from "typedi"; + +@Service() +export default class CustomersService extends BaseService { + constructor(private customerRepository: CustomersRepository) { + super(); + } + + /** + * @description : Get all Customers + * @throws {Error} If Customers cannot be get + */ + public async get(query: Prisma.CustomersFindManyArgs): Promise { + return this.customerRepository.findMany(query); + } + + /** + * @description : Get a customer by uid + * @throws {Error} If customer cannot be get by uid + */ + public async getByUid(uid: string, query?: Prisma.CustomersInclude): Promise { + return this.customerRepository.findOneByUid(uid, query); + } + + /** + * @description : Get a customer by contact uid + * @throws {Error} If customer cannot be get by contact uid + */ + public async getByContact(contactUid: string): Promise { + return this.customerRepository.findOneByContact(contactUid); + } +}