Skip to content

Commit

Permalink
Merge pull request #321 from AppQuality/cp-creation-data
Browse files Browse the repository at this point in the history
Cp creation data
  • Loading branch information
d-beezee authored May 17, 2024
2 parents 6d336ca + 98e0d88 commit dc50891
Show file tree
Hide file tree
Showing 69 changed files with 8,719 additions and 397 deletions.
6 changes: 5 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ GOOGLE_API_KEY=AIzaserejejadejedejebetudejeberesebiuno

PAYMENT_INVOICE_RECAP_CC_EMAIL=it+administration@unguess.io

SENTRY_ENVIRONMENT=local
SENTRY_ENVIRONMENT=local


CAMPAIGN_CREATION_WEBHOOK=https://webhook.site/11111111-1111-1111-1111-11111111111
STATUS_CHANGE_WEBHOOK=https://webhook.site/11111111-1111-1111-1111-11111111111
8 changes: 1 addition & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ RUN yarn global add npm-run-all
RUN yarn build


FROM alpine:3.16 as web

COPY --from=node /usr/lib /usr/lib
COPY --from=node /usr/local/share /usr/local/share
COPY --from=node /usr/local/lib /usr/local/lib
COPY --from=node /usr/local/include /usr/local/include
COPY --from=node /usr/local/bin /usr/local/bin
FROM node:18-alpine3.16 AS web

COPY --from=base /dist /app/build
COPY package*.json /app/
Expand Down
4 changes: 4 additions & 0 deletions deployment/after-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ services:
GOOGLE_API_KEY: '${GOOGLE_API_KEY}'
PAYMENT_INVOICE_RECAP_CC_EMAIL: '${PAYMENT_INVOICE_RECAP_CC_EMAIL}'
SENTRY_ENVIRONMENT: ${ENVIRONMENT}
ENVIRONMENT: ${ENVIRONMENT}
SENTRY_RELEASE: ${DOCKER_IMAGE}
SENTRY_DSN: ${SENTRY_DSN}
SENTRY_SAMPLE_RATE: ${SENTRY_SAMPLE_RATE:-1}
CLOUDFRONT_KEY_ID: ${CLOUDFRONT_KEY_ID}
JOTFORM_APIKEY: ${JOTFORM_APIKEY}
WORDPRESS_API_URL: ${WORDPRESS_API_URL}
CAMPAIGN_CREATION_WEBHOOK: ${CAMPAIGN_CREATION_WEBHOOK}
STATUS_CHANGE_WEBHOOK: ${STATUS_CHANGE_WEBHOOK}
volumes:
- /var/docker/keys:/app/keys
logging:
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@appquality/tryber-database": "^0.27.0",
"@appquality/tryber-database": "^0.39.0",
"@appquality/wp-auth": "^1.0.7",
"@googlemaps/google-maps-services-js": "^3.3.7",
"@sendgrid/mail": "^7.6.0",
Expand All @@ -32,6 +32,7 @@
"body-parser": "^1.19.1",
"codice-fiscale-js": "^2.3.13",
"connect-busboy": "^1.0.0",
"core-js": "^3.6.5",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.18.1",
Expand All @@ -43,6 +44,7 @@
"mysql": "^2.18.1",
"openapi-backend": "^3.9.2",
"parse-comments": "^1.0.0",
"php-serialize": "^4.1.1",
"php-unserialize": "^0.0.1",
"spark-md5": "^3.0.2",
"uuid": "^9.0.0",
Expand Down
7 changes: 7 additions & 0 deletions src/features/webhookTrigger/__mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { iWebhookTrigger } from "../types";

export class WebhookTrigger implements iWebhookTrigger {
async trigger() {
return;
}
}
34 changes: 34 additions & 0 deletions src/features/webhookTrigger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import axios from "axios";
import { WebhookTypes, iWebhookTrigger } from "./types";

export class WebhookTrigger<T extends WebhookTypes["type"]>
implements iWebhookTrigger
{
private webhookUrl: string;
private data: WebhookTypes["data"];

constructor({
type,
data,
}: {
type: T;
data: Extract<WebhookTypes, { type: T }>["data"];
}) {
if (type === "status_change") {
this.webhookUrl = process.env.STATUS_CHANGE_WEBHOOK_URL || "";
} else if (type === "campaign_created") {
this.webhookUrl = process.env.CAMPAIGN_CREATION_WEBHOOK || "";
} else {
throw new Error("Invalid webhook type");
}

this.data = data;
}

async trigger() {
await axios.post(this.webhookUrl, {
...this.data,
environment: process.env.ENVIROMENT,
});
}
}
21 changes: 21 additions & 0 deletions src/features/webhookTrigger/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type StatusChangeWebhook = {
type: "status_change";
data: {
campaignId: number;
newPhase: number;
oldPhase: number;
};
};

type CampaignCreatedWebhook = {
type: "campaign_created";
data: {
campaignId: number;
};
};

export type WebhookTypes = StatusChangeWebhook | CampaignCreatedWebhook;

export interface iWebhookTrigger {
trigger(): Promise<void>;
}
96 changes: 96 additions & 0 deletions src/features/wp/WordpressJsonApiTrigger/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import axios from "axios";
import WordpressJsonApiTrigger from ".";

// mock axios

jest.mock("axios");

describe("WordpressJsonApiTrigger", () => {
beforeAll(() => {
process.env = Object.assign(process.env, {
WORDPRESS_API_URL: "https://example.com",
});
});

afterAll(() => {
jest.resetAllMocks();
process.env = Object.assign(process.env, {
WORDPRESS_API_URL: "",
});
});

afterEach(() => {
jest.clearAllMocks();
});
it("Should create a new instance of WordpressJsonApiTrigger", () => {
const instance = new WordpressJsonApiTrigger(1);
expect(instance).toBeInstanceOf(WordpressJsonApiTrigger);
});

it("Should call axios on generateUseCase", async () => {
const instance = new WordpressJsonApiTrigger(1);

await instance.generateUseCase();

expect(axios).toHaveBeenCalledTimes(1);

expect(axios).toHaveBeenCalledWith({
headers: {
"Content-Type": "application/json",
"User-Agent": "Tryber API",
},
method: "GET",
url: "https://example.com/regenerate-campaign-use-cases/1",
});
});

it("Should call axios on generateMailmerges", async () => {
const instance = new WordpressJsonApiTrigger(1);

await instance.generateMailMerges();

expect(axios).toHaveBeenCalledTimes(1);

expect(axios).toHaveBeenCalledWith({
headers: {
"Content-Type": "application/json",
"User-Agent": "Tryber API",
},
method: "GET",
url: "https://example.com/regenerate-campaign-crons/1",
});
});
it("Should call axios on generatePages", async () => {
const instance = new WordpressJsonApiTrigger(1);

await instance.generatePages();

expect(axios).toHaveBeenCalledTimes(1);

expect(axios).toHaveBeenCalledWith({
headers: {
"Content-Type": "application/json",
"User-Agent": "Tryber API",
},
method: "GET",
url: "https://example.com/regenerate-campaign-pages/1",
});
});

it("Should call axios on generateTasks", async () => {
const instance = new WordpressJsonApiTrigger(1);

await instance.generateTasks();

expect(axios).toHaveBeenCalledTimes(1);

expect(axios).toHaveBeenCalledWith({
headers: {
"Content-Type": "application/json",
"User-Agent": "Tryber API",
},
method: "GET",
url: "https://example.com/regenerate-campaign-tasks/1",
});
});
});
36 changes: 36 additions & 0 deletions src/features/wp/WordpressJsonApiTrigger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import axios from "axios";

class WordpressJsonApiTrigger {
constructor(private campaign: number) {}

public async generateUseCase() {
await this.postToWordpress(
`regenerate-campaign-use-cases/${this.campaign}`
);
}

public async generateMailMerges() {
await this.postToWordpress(`regenerate-campaign-crons/${this.campaign}`);
}

public async generatePages() {
await this.postToWordpress(`regenerate-campaign-pages/${this.campaign}`);
}

public async generateTasks() {
await this.postToWordpress(`regenerate-campaign-tasks/${this.campaign}`);
}

private async postToWordpress(url: string) {
await axios({
method: "GET",
url: `${process.env.WORDPRESS_API_URL}/${url}`,
headers: {
"User-Agent": "Tryber API",
"Content-Type": "application/json",
},
});
}
}

export default WordpressJsonApiTrigger;
Loading

0 comments on commit dc50891

Please sign in to comment.