Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add post customer #311

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/reference/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions src/routes/customers/_post/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
49 changes: 49 additions & 0 deletions src/routes/customers/_post/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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;
22 changes: 22 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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: {
Expand Down
Loading