Skip to content

Commit

Permalink
feat: Update get-campaigns endpoint to include roles
Browse files Browse the repository at this point in the history
  • Loading branch information
d-beezee committed May 6, 2024
1 parent 438565a commit 3520ae1
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/reference/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -758,13 +758,17 @@ paths:
required:
- id
- name
- surname
properties:
id:
type: integer

name:
type: string

surname:
type: string

required:
- role
- user
Expand Down
69 changes: 66 additions & 3 deletions src/routes/campaigns/_get/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const ACCEPTABLE_FIELDS = [
];

type CampaignSelect = ReturnType<typeof tryber.tables.WpAppqEvdCampaign.do>;
type Roles = NonNullable<
NonNullable<
StoplightOperations["get-campaigns"]["responses"]["200"]["content"]["application/json"]["items"]
>[0]["roles"]
>;

class RouteItem extends UserRoute<{
response: StoplightOperations["get-campaigns"]["responses"]["200"]["content"]["application/json"];
Expand Down Expand Up @@ -166,7 +171,7 @@ class RouteItem extends UserRoute<{

query.orderBy(this.orderBy, this.order);

return (await query) as {
const results: {
id?: number;
name?: string;
startDate?: string;
Expand All @@ -186,7 +191,65 @@ class RouteItem extends UserRoute<{
resultType?: -1 | 0 | 1;
phase_id?: number;
phase_name?: string;
}[];
}[] = await query;

const withRoles = this.addRoles(results);

return withRoles;
}

private async addRoles<T extends { id?: number }>(
campaigns: T[]
): Promise<(T & { roles?: Roles })[]> {
if (!this.fields.includes("roles")) return campaigns;
const roles = await tryber.tables.CampaignCustomRoles.do()
.select(
"campaign_id",
"custom_role_id",
tryber.ref("name").withSchema("custom_roles").as("custom_role_name"),
"tester_id",
tryber.ref("name").withSchema("wp_appq_evd_profile").as("tester_name"),
tryber
.ref("surname")
.withSchema("wp_appq_evd_profile")
.as("tester_surname")
)
.join(
"custom_roles",
"custom_roles.id",
"campaign_custom_roles.custom_role_id"
)
.join(
"wp_appq_evd_profile",
"wp_appq_evd_profile.id",
"campaign_custom_roles.tester_id"
)
.whereIn(
"campaign_id",
campaigns.map((result) => result.id || 0)
);

return campaigns.map((campaign) => {
const rolesForCampaign = roles.filter(
(role) => role.campaign_id === campaign.id
);
const results = rolesForCampaign.map((role) => ({
role: {
id: role.custom_role_id,
name: role.custom_role_name,
},
user: {
id: role.tester_id,
name: role.tester_name,
surname: role.tester_surname,
},
}));

return {
...campaign,
roles: results,
};
});
}

private formatCampaigns(
Expand Down Expand Up @@ -252,7 +315,7 @@ class RouteItem extends UserRoute<{
resultType: this.getResultTypeName(campaign.resultType),

...(this.fields.includes("roles") && {
roles: [],
roles: campaign.roles,
}),
}));
}
Expand Down
109 changes: 109 additions & 0 deletions src/routes/campaigns/_get/roles.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import app from "@src/app";
import { tryber } from "@src/features/database";
import request from "supertest";

describe("GET /campaigns - roles", () => {
beforeAll(async () => {
await tryber.tables.WpAppqEvdCampaign.do().insert({
id: 1,
platform_id: 1,
start_date: "2023-01-13 10:10:10",
end_date: "2023-01-14 10:10:10",
title: "This is the title",
page_preview_id: 1,
page_manual_id: 1,
customer_id: 1,
pm_id: 1,
project_id: 1,
customer_title: "",
campaign_pts: 200,
});

await tryber.tables.WpAppqEvdProfile.do().insert({
id: 1,
name: "User",
surname: "Name",
email: "",
wp_user_id: 1,
education_id: 1,
employment_id: 1,
});

await tryber.tables.CustomRoles.do().insert({
id: 1,
name: "Role",
olp: "",
});

await tryber.tables.CampaignCustomRoles.do().insert({
id: 1,
campaign_id: 1,
custom_role_id: 1,
tester_id: 1,
});
});

afterAll(async () => {
await tryber.tables.WpAppqEvdCampaign.do().delete();
await tryber.tables.WpAppqEvdProfile.do().delete();
await tryber.tables.CustomRoles.do().delete();
await tryber.tables.CampaignCustomRoles.do().delete();
});

it("Should return roles", async () => {
const response = await request(app)
.get("/campaigns")
.set("Authorization", 'Bearer tester olp {"appq_campaign":true}');
expect(response.status).toBe(200);

expect(response.body).toHaveProperty("items");
const { items } = response.body;
expect(items).toHaveLength(1);

expect(items[0]).toHaveProperty("roles");

const { roles } = items[0];

expect(roles).toEqual([
{
role: { id: 1, name: "Role" },
user: { id: 1, name: "User", surname: "Name" },
},
]);
});

it("Should not return roles if not in fields", async () => {
const response = await request(app)
.get("/campaigns?fields=id")
.set("Authorization", 'Bearer tester olp {"appq_campaign":true}');
expect(response.status).toBe(200);

expect(response.body).toHaveProperty("items");
const { items } = response.body;
expect(items).toHaveLength(1);

expect(items[0]).not.toHaveProperty("roles");
});

it("Should return roles if in fields", async () => {
const response = await request(app)
.get("/campaigns?fields=roles")
.set("Authorization", 'Bearer tester olp {"appq_campaign":true}');
expect(response.status).toBe(200);

expect(response.body).toHaveProperty("items");
const { items } = response.body;
expect(items).toHaveLength(1);

expect(items[0]).toHaveProperty("roles");

const { roles } = items[0];

expect(roles).toEqual([
{
role: { id: 1, name: "Role" },
user: { id: 1, name: "User", surname: "Name" },
},
]);
});
});
1 change: 1 addition & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,7 @@ export interface operations {
user: {
id: number;
name: string;
surname: string;
};
}[];
}[];
Expand Down

0 comments on commit 3520ae1

Please sign in to comment.