-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #321 from AppQuality/cp-creation-data
Cp creation data
- Loading branch information
Showing
69 changed files
with
8,719 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.