From 4c2bc9120f0291e9d9b6882256e6fd7aac23878b Mon Sep 17 00:00:00 2001 From: d-beezee <59012086+d-beezee@users.noreply.github.com> Date: Mon, 29 Apr 2024 12:40:25 +0200 Subject: [PATCH] feat: Add post customer (#311) --- src/reference/openapi.yml | 36 ++++++++++++++ src/routes/customers/_post/index.spec.ts | 60 ++++++++++++++++++++++++ src/routes/customers/_post/index.ts | 49 +++++++++++++++++++ src/schema.ts | 22 +++++++++ 4 files changed, 167 insertions(+) create mode 100644 src/routes/customers/_post/index.spec.ts create mode 100644 src/routes/customers/_post/index.ts diff --git a/src/reference/openapi.yml b/src/reference/openapi.yml index 44e2e06fd..2377d5c16 100644 --- a/src/reference/openapi.yml +++ b/src/reference/openapi.yml @@ -4136,6 +4136,42 @@ paths: security: - JWT: [] parameters: [] + post: + summary: '' + operationId: post-customers + responses: + '200': + description: OK + content: + application/json: + schema: + type: object + properties: + id: + type: integer + x-stoplight: + id: zxshcfdjbugtr + name: + type: string + x-stoplight: + id: s32ugpcfzhrmp + required: + - id + - name + security: + - JWT: [] + requestBody: + content: + application/json: + schema: + type: object + properties: + name: + type: string + x-stoplight: + id: 5gyi3swml7tyw + required: + - name /custom_user_fields: get: summary: Get all custom user fields diff --git a/src/routes/customers/_post/index.spec.ts b/src/routes/customers/_post/index.spec.ts new file mode 100644 index 000000000..7ebe878c2 --- /dev/null +++ b/src/routes/customers/_post/index.spec.ts @@ -0,0 +1,60 @@ +import app from "@src/app"; +import { tryber } from "@src/features/database"; +import request from "supertest"; + +describe("POST /customers", () => { + afterEach(async () => { + await tryber.tables.WpAppqCustomer.do().delete(); + }); + + it("Should answer 403 if not logged in", () => { + return request(app) + .post("/customers") + .send({ name: "New project" }) + .expect(403); + }); + it("Should answer 403 if logged in without permissions", async () => { + const response = await request(app) + .post("/customers") + .send({ name: "New project" }) + .set("Authorization", "Bearer tester"); + expect(response.status).toBe(403); + }); + it("Should answer 201 if logged as user with full access on campaigns", async () => { + const response = await request(app) + .post("/customers") + .send({ name: "New project" }) + .set("Authorization", 'Bearer tester olp {"appq_campaign":true}'); + expect(response.status).toBe(201); + }); + it("Should answer 403 if logged as user with access to some campaigns", async () => { + const response = await request(app) + .post("/customers") + .send({ name: "New project" }) + .set("Authorization", 'Bearer tester olp {"appq_campaign":[1,2]}'); + expect(response.status).toBe(403); + }); + + it("Should add customer", async () => { + const postResponse = await request(app) + .post("/customers") + .send({ name: "New project" }) + .set("Authorization", "Bearer admin"); + + expect(postResponse.status).toBe(201); + expect(postResponse.body).toHaveProperty("id"); + expect(postResponse.body).toHaveProperty("name"); + const { id, name } = postResponse.body; + + const getResponse = await request(app) + .get("/customers") + .set("Authorization", "Bearer admin"); + + expect(getResponse.status).toBe(200); + + const customers = getResponse.body; + expect(customers).toHaveLength(1); + expect(customers[0].id).toBe(id); + expect(customers[0].name).toBe(name); + }); +}); diff --git a/src/routes/customers/_post/index.ts b/src/routes/customers/_post/index.ts new file mode 100644 index 000000000..7dccd76f4 --- /dev/null +++ b/src/routes/customers/_post/index.ts @@ -0,0 +1,49 @@ +/** OPENAPI-CLASS : post-customers */ + +import OpenapiError from "@src/features/OpenapiError"; +import { tryber } from "@src/features/database"; +import UserRoute from "@src/features/routes/UserRoute"; + +class RouteItem extends UserRoute<{ + response: StoplightOperations["post-customers"]["responses"]["200"]["content"]["application/json"]; + body: StoplightOperations["post-customers"]["requestBody"]["content"]["application/json"]; +}> { + private accessibleCampaigns: true | number[] = this.campaignOlps + ? this.campaignOlps + : []; + + protected async filter() { + if ((await super.filter()) === false) return false; + if (this.doesNotHaveAccessToCampaigns()) { + this.setError(403, new OpenapiError("You are not authorized to do this")); + return false; + } + return true; + } + + private doesNotHaveAccessToCampaigns() { + return this.accessibleCampaigns !== true; + } + + protected async prepare(): Promise { + const customer = await this.createCustomer(); + return this.setSuccess(201, customer); + } + + private async createCustomer() { + const customer = await tryber.tables.WpAppqCustomer.do() + .insert({ + company: this.getBody().name, + pm_id: 0, + }) + .returning("id"); + const id = customer[0].id ?? customer[0]; + + return { + id: id, + name: this.getBody().name, + }; + } +} + +export default RouteItem; diff --git a/src/schema.ts b/src/schema.ts index eeb13b76f..c90b2c4df 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -224,6 +224,7 @@ export interface paths { "/customers": { /** Get all the customers you have access to */ get: operations["get-customers"]; + post: operations["post-customers"]; parameters: {}; }; "/custom_user_fields": { @@ -2255,6 +2256,27 @@ export interface operations { 403: components["responses"]["NotFound"]; }; }; + "post-customers": { + parameters: {}; + responses: { + /** OK */ + 200: { + content: { + "application/json": { + id: number; + name: string; + }; + }; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + }; + }; + }; + }; "get-customUserFields": { parameters: {}; responses: {