From 1f1f4b9f4ed1db3eb2fc73717f9432c1a094e67c Mon Sep 17 00:00:00 2001 From: sinatragianpaolo Date: Tue, 26 Nov 2024 03:34:20 +0100 Subject: [PATCH 1/9] feat: create demoenvironment on post customer --- .../copyUxData/index.spec.ts | 68 +++ .../createDemoEnvironment/copyUxData/index.ts | 44 ++ .../createCpExperiential/index.spec.ts | 543 ++++++++++++++++++ .../createCpExperiential/index.ts | 344 +++++++++++ .../createCpFunctional/index.spec.ts | 333 +++++++++++ .../createCpFunctional/index.ts | 119 ++++ .../createDemoProject/index.spec.ts | 53 ++ .../createDemoProject/index.ts | 20 + .../getCpData/index.spec.ts | 315 ++++++++++ .../createDemoEnvironment/getCpData/index.ts | 78 +++ .../getExperientialData/index.spec.ts | 408 +++++++++++++ .../getExperientialData/index.ts | 120 ++++ .../_post/createDemoEnvironment/index.ts | 49 ++ .../workspaceExist/index.spec.ts | 25 + .../workspaceExist/index.ts | 16 + src/routes/customers/_post/index.ts | 5 + 16 files changed, 2540 insertions(+) create mode 100644 src/routes/customers/_post/createDemoEnvironment/copyUxData/index.spec.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/copyUxData/index.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.spec.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.spec.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.spec.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/getCpData/index.spec.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/getCpData/index.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.spec.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/index.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.spec.ts create mode 100644 src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.ts diff --git a/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.spec.ts new file mode 100644 index 000000000..d96c1790b --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.spec.ts @@ -0,0 +1,68 @@ +import { copyUxData } from "."; +import { tryber } from "@src/features/database"; + +describe("copyUxData", () => { + const sourceCampaignId = 69; + + beforeAll(async () => { + await tryber.tables.UxCampaignData.do().insert([ + { + id: 1, + campaign_id: sourceCampaignId, + published: 1, + methodology_type: "metodology1", + methodology_description: "methodology_description", + goal: "goal", + users: 15, + }, + { + id: 2, + campaign_id: 9999, + published: 1, + methodology_type: "metodology1", + methodology_description: "methodology_description", + goal: "goal", + users: 15, + }, + ]); + await tryber.tables.UxCampaignQuestions.do().insert([ + { id: 1, campaign_id: sourceCampaignId, question: "question1" }, + { id: 2, campaign_id: sourceCampaignId, question: "question2" }, + ]); + }); + afterAll(async () => { + await tryber.tables.UxCampaignData.do().delete(); + await tryber.tables.UxCampaignQuestions.do().delete(); + }); + it("Should copy the ux data from source to target campaign", async () => { + const targetCpId = 12345; + await copyUxData({ sourceCpId: sourceCampaignId, targetCpId: targetCpId }); + const columnUx = [ + "published", + "methodology_type", + "methodology_description", + "goal", + "users", + ]; + const sourceData = await tryber.tables.UxCampaignData.do() + .select(columnUx) + .where("campaign_id", sourceCampaignId); + const targetData = await tryber.tables.UxCampaignData.do() + .select(columnUx) + .where("campaign_id", targetCpId); + expect(targetData).toHaveLength(1); + expect(targetData).toMatchObject(sourceData); + }); + it("Should copy the questions from source to target campaign", async () => { + const targetCpId = 54321; + await copyUxData({ sourceCpId: sourceCampaignId, targetCpId: targetCpId }); + const sourceQuestions = await tryber.tables.UxCampaignQuestions.do() + .select("question") + .where("campaign_id", sourceCampaignId); + const targetQuestions = await tryber.tables.UxCampaignQuestions.do() + .select("question") + .where("campaign_id", targetCpId); + expect(targetQuestions).toHaveLength(sourceQuestions.length); + expect(targetQuestions).toMatchObject(sourceQuestions); + }); +}); diff --git a/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.ts b/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.ts new file mode 100644 index 000000000..32cde5e4a --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.ts @@ -0,0 +1,44 @@ +import { tryber } from "@src/features/database"; + +// ux - UxCampaignData +// questions - UxCampaignQuestions +const copyUxData = async ({ + sourceCpId, + targetCpId, +}: { + sourceCpId: number; + targetCpId: number; +}) => { + const sourceData = await tryber.tables.UxCampaignData.do() + .select( + "published", + "methodology_type", + "methodology_description", + "goal", + "users" + ) + .where("campaign_id", sourceCpId) + .first(); + + if (sourceData) { + await tryber.tables.UxCampaignData.do().insert({ + ...sourceData, + campaign_id: targetCpId, + }); + } + + const sourceQuestions = await tryber.tables.UxCampaignQuestions.do() + .select("question") + .where("campaign_id", sourceCpId); + + if (sourceQuestions.length) { + await tryber.tables.UxCampaignQuestions.do().insert( + sourceQuestions.map((question) => ({ + ...question, + campaign_id: targetCpId, + })) + ); + } +}; + +export { copyUxData }; diff --git a/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.spec.ts new file mode 100644 index 000000000..ced0ac4f4 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.spec.ts @@ -0,0 +1,543 @@ +import { createCpExperiential } from "."; +import { tryber } from "@src/features/database"; + +describe("createCpExperiential", () => { + const targetCampaignId = 69; + const targetPorjectId = 6969; + const transcript_1 = `{ + "speakers": 2, + "paragraphs": [ + { + "end": 0.65999997, + "text": "Bene?", + "start": 0.16, + "words": [ + { + "end": 0.65999997, + "word": "Bene?", + "start": 0.16, + "speaker": 0 + } + ], + "speaker": 0 + }, { + "end": 2.6599998, + "text": "Sì la vedo la sento bene", + "start": 0.88, + "words": [ + { + "end": 1.1999999, + "word": "Sì", + "start": 0.88, + "speaker": 1 + }, { + "end": 1.4399999, + "word": "la", + "start": 1.1999999, + "speaker": 1 + }, { + "end": 1.5999999, + "word": "vedo", + "start": 1.4399999, + "speaker": 1 + }, { + "end": 1.8399999, + "word": "la", + "start": 1.5999999, + "speaker": 1 + } + ] + } + ] + }`; + + beforeAll(async () => { + await tryber.tables.WpAppqEvdCampaign.do().insert({ + id: targetCampaignId, + title: "Title - Functional Campaign", + description: "Functional Campaign", + campaign_type: 1, + campaign_type_id: 1, + project_id: 6969, + platform_id: 1, + start_date: "2021-01-01", + end_date: "2021-01-01", + page_preview_id: 1, + page_manual_id: 1, + pm_id: 1, + customer_id: 1, + customer_title: "Customer Title - Functional Campaign", + }); + await tryber.tables.WpAppqCampaignTask.do().insert([ + { + id: 10, + campaign_id: targetCampaignId, + title: "Title - Campaign Task10", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + { + id: 20, + campaign_id: 70, + title: "Title - Campaign Task20", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + { + id: 999, + campaign_id: 9999, + title: "Title - Campaign Task other cp", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + ]); + await tryber.tables.WpAppqCampaignTaskGroup.do().insert([ + { + task_id: 10, + group_id: 1, + }, + { + task_id: 20, + group_id: 0, + }, + { + task_id: 999, + group_id: 0, + }, + ]); + await tryber.tables.WpAppqUserTask.do().insert([ + { + id: 100, + task_id: 10, + tester_id: 11111, + is_completed: 1, + }, + { + id: 101, + task_id: 10, + tester_id: 32, + is_completed: 0, + }, + { + id: 102, + task_id: 999, + tester_id: 32, + is_completed: 1, + }, + ]); + await tryber.tables.WpCrowdAppqHasCandidate.do().insert([ + { + user_id: 11111, + campaign_id: targetCampaignId, + }, + { + user_id: 32, + campaign_id: targetCampaignId, + }, + { + user_id: 32, + campaign_id: 9999, + }, + ]); + await tryber.tables.WpAppqUserTaskMedia.do().insert([ + { + id: 10, + campaign_task_id: 10, + user_task_id: 100, + tester_id: 11111, + location: "location10", + }, + { + id: 20, + campaign_task_id: 20, + user_task_id: 101, + tester_id: 11111, + location: "location20", + }, + { + id: 999, + campaign_task_id: 999, + user_task_id: 100, + tester_id: 32, + location: "location999", + }, + ]); + await tryber.tables.WpAppqUsecaseMediaObservations.do().insert([ + { + id: 10, + media_id: 10, + video_ts: 10, + video_ts_end: 20, + name: "name", + description: "description", + ux_note: "ux_note", + }, + { + id: 20, + media_id: 20, + video_ts: 10, + video_ts_end: 30, + name: "name", + description: "description", + ux_note: "ux_note", + }, + { + id: 999, + media_id: 999, + video_ts: 10, + video_ts_end: 30, + name: "name", + description: "description", + ux_note: "ux_note", + }, + ]); + await tryber.tables.UxCampaignData.do().insert([ + { + id: 1, + campaign_id: targetCampaignId, + published: 1, + methodology_type: "metodology1", + methodology_description: "methodology_description", + goal: "goal", + users: 15, + }, + { + id: 2, + campaign_id: 9999, + published: 1, + methodology_type: "metodology1", + methodology_description: "methodology_description", + goal: "goal", + users: 15, + }, + ]); + await tryber.tables.UxCampaignSentiments.do().insert([ + { + id: 1, + campaign_id: targetCampaignId, + cluster_id: 10, + value: 5, + comment: "comment", + }, + { + id: 2, + campaign_id: 999, + cluster_id: 99, + value: 1, + comment: "comment2", + }, + ]); + await tryber.tables.MediaTranscripts.do().insert([ + { + id: 11, + media_id: 10, + transcript: transcript_1, + language: "it", + }, + { + id: 22, + media_id: 20, + transcript: transcript_1, + language: "en", + }, + { + id: 999, + media_id: 999, + transcript: transcript_1, + language: "zh", + }, + ]); + }); + afterAll(async () => { + await tryber.tables.WpAppqEvdCampaign.do().delete(); + await tryber.tables.WpAppqCampaignTask.do().delete(); + await tryber.tables.WpAppqCampaignTaskGroup.do().delete(); + await tryber.tables.WpAppqUserTask.do().delete(); + await tryber.tables.WpCrowdAppqHasCandidate.do().delete(); + await tryber.tables.WpAppqUserTaskMedia.do().delete(); + await tryber.tables.WpAppqUsecaseMediaObservations.do().delete(); + await tryber.tables.UxCampaignData.do().delete(); + await tryber.tables.UxCampaignSentiments.do().delete(); + }); + + it("Should insert one row in wp_appq_evd_campaigns", async () => { + const cpDatasBefore = await tryber.tables.WpAppqEvdCampaign.do().select(); + await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + const cpDatasAfter = await tryber.tables.WpAppqEvdCampaign.do().select(); + expect(cpDatasAfter.length).toEqual(cpDatasBefore.length + 1); + }); + it("Return id 0 if the source campaign does not exist", async () => { + const cpId = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: 99999, + }); + expect(cpId).toMatchObject({ + cpIdExperiential: 0, + }); + }); + it("Should insert the experiential campaign with same data the target campaign except projectId", async () => { + const sourceCpData = await tryber.tables.WpAppqEvdCampaign.do() + .select() + .where("id", targetCampaignId) + .first(); + const newCp = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + const newCpData = await tryber.tables.WpAppqEvdCampaign.do() + .select() + .where("id", newCp.cpIdExperiential) + .first(); + expect(newCpData).toEqual({ + ...sourceCpData, + id: newCp.cpIdExperiential, + }); + }); + it("Should return the id of the inserted campaign", async () => { + const newCp = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + expect(newCp).toMatchObject({ + cpIdExperiential: expect.any(Number), + }); + expect(newCp.cpIdExperiential).toBeGreaterThan(0); + }); + it("Should insert the experiential campaign with projectId equal to target project id", async () => { + const newCp = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + const newCpData = await tryber.tables.WpAppqEvdCampaign.do() + .select("id", "project_id") + .where("id", newCp.cpIdExperiential) + .first(); + expect(newCpData).toEqual({ + id: newCp.cpIdExperiential, + project_id: targetPorjectId, + }); + }); + it("Should insert uxData equal to source data with updated campaignId", async () => { + const uxDataBefore = await tryber.tables.UxCampaignData.do().select(); + const newCp = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + const uxDataAfter = await tryber.tables.UxCampaignData.do().select(); + expect(uxDataAfter.length).toBeGreaterThan(uxDataBefore.length); + + const uxColumns = [ + "goal", + "methodology_description", + "methodology_type", + "published", + "users", + ]; + const sourceUxData = await tryber.tables.UxCampaignData.do() + .select(uxColumns) + .where("campaign_id", targetCampaignId) + .first(); + const newUxData = await tryber.tables.UxCampaignData.do() + .select(uxColumns) + .where("campaign_id", newCp.cpIdExperiential) + .first(); + expect(newUxData).toMatchObject(sourceUxData ?? {}); + }); + it("Should insert sentiments", async () => { + const sentimentBefore = + await tryber.tables.UxCampaignSentiments.do().select(); + const newpc = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + const sentimentAfter = + await tryber.tables.UxCampaignSentiments.do().select(); + expect(sentimentAfter.length).toBeGreaterThan(sentimentBefore.length); + + const sourceSentiment = await tryber.tables.UxCampaignSentiments.do() + .select("value", "comment") + .where("campaign_id", targetCampaignId); + expect(sourceSentiment.length).toBe(1); + const newSentiment = await tryber.tables.UxCampaignSentiments.do() + .select("value", "comment") + .where("campaign_id", newpc.cpIdExperiential); + expect(newSentiment.length).toBe(1); + expect(newSentiment[0]).toMatchObject(sourceSentiment[0]); + }); + it("Should insert campaignTasks", async () => { + const campaignTasksBefore = + await tryber.tables.WpAppqCampaignTask.do().select(); + const newpc = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + const campaignTasksAfter = + await tryber.tables.WpAppqCampaignTask.do().select(); + expect(campaignTasksAfter.length).toBeGreaterThan( + campaignTasksBefore.length + ); + const usecasesColumns = [ + "title", + "content", + "is_required", + "simple_title", + "info", + "prefix", + ]; + const sourceCampaignTasks = await tryber.tables.WpAppqCampaignTask.do() + .select(usecasesColumns) + .where("campaign_id", targetCampaignId); + expect(sourceCampaignTasks.length).toBe(1); + const newCampaignTasks = await tryber.tables.WpAppqCampaignTask.do() + .select(usecasesColumns) + .where("campaign_id", newpc.cpIdExperiential); + expect(newCampaignTasks.length).toBe(1); + expect(newCampaignTasks).toMatchObject(sourceCampaignTasks); + }); + it("Should insert userTasks", async () => { + const userTasksBefore = await tryber.tables.WpAppqUserTask.do().select(); + const newpc = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + const userTasksAfter = await tryber.tables.WpAppqUserTask.do().select(); + expect(userTasksAfter.length).toBeGreaterThan(userTasksBefore.length); + const usecasesColumns = ["tester_id", "is_completed"]; + const sourceUserTasks = await tryber.tables.WpAppqUserTask.do() + .select(usecasesColumns) + .whereIn("task_id", [10]); + expect(sourceUserTasks.length).toBe(2); + const newUsecases = ( + await tryber.tables.WpAppqCampaignTask.do() + .select("id") + .where("campaign_id", newpc.cpIdExperiential) + ).map((ct) => ct.id); + const newUserTasks = await tryber.tables.WpAppqUserTask.do() + .select(usecasesColumns) + .whereIn("task_id", newUsecases); + expect(newUserTasks.length).toBe(2); + expect(newUserTasks).toMatchObject(sourceUserTasks); + }); + it("Should insert userTaskMedia", async () => { + const userTaskMediaBefore = + await tryber.tables.WpAppqUserTaskMedia.do().select(); + const newpc = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + //78 + const userTaskMediaAfter = + await tryber.tables.WpAppqUserTaskMedia.do().select(); + expect(userTaskMediaAfter.length).toBeGreaterThan( + userTaskMediaBefore.length + ); + const videoColumns = ["tester_id", "location"]; + const sourceUserTaskMedia = await tryber.tables.WpAppqUserTaskMedia.do() + .select(videoColumns) + .where("campaign_task_id", 10); + expect(sourceUserTaskMedia.length).toBe(1); + const newUsecases = ( + await tryber.tables.WpAppqCampaignTask.do() + .select("id") + .where("campaign_id", newpc.cpIdExperiential) + ).map((ct) => ct.id); + const newUserTaskMedia = await tryber.tables.WpAppqUserTaskMedia.do() + .select(videoColumns) + .whereIn("campaign_task_id", newUsecases); + expect(newUserTaskMedia.length).toBe(1); + expect(newUserTaskMedia[0]).toMatchObject(sourceUserTaskMedia[0]); + }); + + it("Should insert observations", async () => { + const observationsBefore = + await tryber.tables.WpAppqUsecaseMediaObservations.do().select(); + const newpc = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + const observationsAfter = + await tryber.tables.WpAppqUsecaseMediaObservations.do().select(); + expect(observationsAfter.length).toBeGreaterThan(observationsBefore.length); + + const mediaColumns = [ + "video_ts", + "video_ts_end", + "name", + "description", + "ux_note", + ]; + const sourceObservations = + await tryber.tables.WpAppqUsecaseMediaObservations.do() + .select(mediaColumns) + .where("media_id", 10); + expect(sourceObservations.length).toBe(1); + const newCampaignTasksIds = ( + await tryber.tables.WpAppqCampaignTask.do() + .select("id") + .where("campaign_id", newpc.cpIdExperiential) + ).map((ct) => ct.id); + const newMediaIds = ( + await tryber.tables.WpAppqUserTaskMedia.do() + .select("id") + .whereIn("campaign_task_id", newCampaignTasksIds) + ).map((ct) => ct.id); + const newObservations = + await tryber.tables.WpAppqUsecaseMediaObservations.do() + .select(mediaColumns) + .whereIn("media_id", newMediaIds); + expect(newObservations.length).toBe(1); + expect(newObservations).toMatchObject(sourceObservations); + }); + + it("Should insert transcritps", async () => { + const transcriptsBefore = + await tryber.tables.MediaTranscripts.do().select(); + const newpc = await createCpExperiential({ + projectId: targetPorjectId, + sourceCpId: targetCampaignId, + }); + const transcriptsAfter = await tryber.tables.MediaTranscripts.do().select(); + expect(transcriptsAfter.length).toBeGreaterThan(transcriptsBefore.length); + + const newCampaignTasksIds = ( + await tryber.tables.WpAppqCampaignTask.do() + .select("id") + .where("campaign_id", newpc.cpIdExperiential) + ).map((ct) => ct.id); + const newMediaIds = ( + await tryber.tables.WpAppqUserTaskMedia.do() + .select("id") + .whereIn("campaign_task_id", newCampaignTasksIds) + ).map((ct) => ct.id); + const sourceTranscripts = await tryber.tables.MediaTranscripts.do() + .select("transcript", "language") + .whereIn("media_id", [10]); + expect(sourceTranscripts.length).toBe(1); + const newTranscripts = await tryber.tables.MediaTranscripts.do() + .select("transcript", "language") + .whereIn("media_id", newMediaIds); + expect(newTranscripts.length).toBe(1); + expect(newTranscripts).toMatchObject(sourceTranscripts); + }); + // it("Should insert translations", async () => {}); + // it("Should insert tagGroups", async () => {}); + // it("Should insert tags", async () => {}); +}); diff --git a/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.ts b/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.ts new file mode 100644 index 000000000..450284581 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.ts @@ -0,0 +1,344 @@ +import { copyUxData } from "../copyUxData"; +import { tryber } from "@src/features/database"; +import { getCpData } from "../getCpData"; +import { getExperientialData } from "../getExperientialData"; + +async function createCpExperiential({ + projectId, + sourceCpId, +}: { + projectId: number; + sourceCpId: number; +}): Promise<{ cpIdExperiential: number }> { + const data = await getCpData({ cpId: sourceCpId }); + + if (!data.campaign) return { cpIdExperiential: 0 }; + const taggingToolData = await getExperientialData({ cpId: sourceCpId }); + + const oldCpId = data.campaign.id; + data.campaign.id = undefined; + + const cpIdExperiential = await insertCampaign(); + + data.campaign.id = cpIdExperiential; + data.campaign.oldCpId = oldCpId; + + // IDs mapping OLD_ID -> NEW_ID + const usecaseMap = new Map(); + const userTaskMap = new Map(); + const mediaMap = new Map(); + const tagsTypeMap = new Map(); + const tagsMap = new Map(); + const observationMap = new Map(); + const insightsMap = new Map(); + + if (cpIdExperiential) { + // Basic Datas + await insertCandidates(); + await insertUseCases(); + await insertUserTasks(); + await insertBugs(); + + // Tagging Tool Data + await insertVideosWithTranscriptAndTranslations(); + await insertTagTypes(); + await insertTags(); + await insertObservations(); + await insertTagLinks(); + await copyInsights(); + + // Copy UX Data - ux, questions + await copyUxData({ sourceCpId: sourceCpId, targetCpId: cpIdExperiential }); + // Copy Sentiments + await copySentiments(); + } + + return { cpIdExperiential }; + + async function copySentiments() { + const sentiment = await tryber.tables.UxCampaignSentiments.do() + .select() + .where("campaign_id", sourceCpId); + + if (sentiment.length) { + for (const sent of sentiment) { + if (!usecaseMap.get(sent.cluster_id)) { + console.log( + "error on upload sentiment, not found cluster:id:", + sent.cluster_id + ); + } + await tryber.tables.UxCampaignSentiments.do().insert({ + ...sent, + id: undefined, + campaign_id: cpIdExperiential, + cluster_id: usecaseMap.get(sent.cluster_id), + }); + } + } + } + + async function insertCampaign() { + const newCampaign = await tryber.tables.WpAppqEvdCampaign.do() + .insert({ ...data.campaign, project_id: projectId }) + .returning("id"); + const cpIdExperiential = newCampaign[0].id ?? newCampaign[0]; + return cpIdExperiential ?? 0; + } + + async function insertBugs() { + if (data.bugs) { + for (const bug of data.bugs) { + bug.campaign_id = cpIdExperiential; + const oldBugId = bug.id; + bug.id = undefined; + const usecaseRef = data.usecases?.find( + (usecase) => usecase.oldUsecaseId === bug.application_section_id + ); + bug.application_section = usecaseRef?.title; + bug.application_section_id = usecaseRef?.id; + const newBug = await tryber.tables.WpAppqEvdBug.do() + .insert(bug) + .returning("id"); + const newBugId = newBug[0].id ?? newBug[0]; + bug.id = newBugId; + bug.oldBugId = oldBugId; + + if (data.bugMedias) { + for (const bugMedia of data.bugMedias) { + if (bugMedia.bug_id === bug.oldBugId) { + const oldBugMediaId = bugMedia.id; + bugMedia.bug_id = newBugId; + bugMedia.id = undefined; + const newBugMedia = await tryber.tables.WpAppqEvdBugMedia.do() + .insert(bugMedia) + .returning("id"); + bugMedia.oldBugMediaId = oldBugMediaId; + bugMedia.id = newBugMedia[0].id ?? newBugMedia[0]; + } + } + } + } + } + } + + async function insertUseCases() { + if (!data.usecases) return; + + for (const uc of data.usecases) { + const oldUsecaseId = uc.id; + uc.id = undefined; + uc.campaign_id = cpIdExperiential; + + const newUseCase = await tryber.tables.WpAppqCampaignTask.do() + .insert(uc) + .returning("id"); + const newUseCaseId = newUseCase[0].id ?? newUseCase[0]; + + usecaseMap.set(oldUsecaseId, newUseCaseId); + } + + return true; + } + + async function insertUserTasks() { + if (!data.userTasks) return; + + for (const ut of data.userTasks) { + const oldUserTaskId = ut.id; + ut.id = undefined; + ut.task_id = usecaseMap.get(ut.task_id); + + const newUserTask = await tryber.tables.WpAppqUserTask.do() + .insert(ut) + .returning("id"); + const newUserTaskId = newUserTask[0].id ?? newUserTask[0]; + + userTaskMap.set(oldUserTaskId, newUserTaskId); + } + + return true; + } + + async function insertCandidates() { + if (data.candidates) { + for (const candidate of data.candidates) { + candidate.campaign_id = cpIdExperiential; + await tryber.tables.WpCrowdAppqHasCandidate.do().insert(candidate); + } + } + } + + async function insertVideosWithTranscriptAndTranslations() { + if (!taggingToolData.userTasksMedia) return; + + for (const video of taggingToolData.userTasksMedia) { + const oldVideoId = video.id; + + video.id = undefined; + video.campaign_task_id = usecaseMap.get(video.campaign_task_id); + video.user_task_id = userTaskMap.get(video.user_task_id); + const newVideo = await tryber.tables.WpAppqUserTaskMedia.do() + .insert(video) + .returning("id"); + const newVideoId = newVideo[0].id ?? newVideo[0]; + + mediaMap.set(oldVideoId, newVideoId); + + // Insert Media Transcripts + const trans = await tryber.tables.MediaTranscripts.do() + .select() + .where("media_id", oldVideoId) + .first(); + + if (trans) { + await tryber.tables.MediaTranscripts.do().insert({ + ...trans, + id: undefined, + media_id: newVideoId, + }); + } + + /** + * Check if there are any translations for this video + */ + const translation = await tryber.tables.MediaTranscriptsTranslations.do() + .select() + .where("media_id", oldVideoId) + .first(); + + if (translation) { + await tryber.tables.MediaTranscriptsTranslations.do().insert({ + ...translation, + id: undefined, + media_id: newVideoId, + }); + } + } + + return true; + } + + async function insertTagTypes() { + if (!taggingToolData.tagTypes) return; + + for (const tagType of taggingToolData.tagTypes) { + const oldTagTypeId = tagType.id; + + tagType.id = undefined; + tagType.campaign_id = cpIdExperiential; + + const newTagType = await tryber.tables.WpAppqUsecaseMediaTagType.do() + .insert(tagType) + .returning("id"); + + const newTagTypeId = newTagType[0].id ?? newTagType[0]; + tagsTypeMap.set(oldTagTypeId, newTagTypeId); + } + + return true; + } + + async function insertTags() { + if (!taggingToolData.tags) return; + + for (const tag of taggingToolData.tags) { + const oldTagId = tag.id; + + tag.id = undefined; + tag.type = tagsTypeMap.get(tag.type); + + const newTag = await tryber.tables.WpAppqUsecaseMediaObservationsTags.do() + .insert(tag) + .returning("id"); + + const newTagId = newTag[0].id ?? newTag[0]; + tagsMap.set(oldTagId, newTagId); + } + + return true; + } + + async function insertObservations() { + if (!taggingToolData.observations) return; + + for (const observation of taggingToolData.observations) { + const oldObservationId = observation.id; + + observation.id = undefined; + observation.media_id = mediaMap.get(observation.media_id); + + const newObservation = + await tryber.tables.WpAppqUsecaseMediaObservations.do() + .insert(observation) + .returning("id"); + + observation.id = newObservation[0].id ?? newObservation[0]; + observationMap.set(oldObservationId, observation.id); + } + + return true; + } + + async function insertTagLinks() { + if (!taggingToolData.tagLinks) return; + + for (const tagLink of taggingToolData.tagLinks) { + tagLink.id = undefined; + tagLink.tag_id = tagsMap.get(tagLink.tag_id); + tagLink.observation_id = observationMap.get(tagLink.observation_id); + + await tryber.tables.WpAppqUsecaseMediaObservationsTagsLink.do().insert( + tagLink + ); + } + + return true; + } + + async function copyInsights() { + const insights = await tryber.tables.UxCampaignInsights.do() + .select() + .where("campaign_id", sourceCpId); + + if (!insights.length) return; + + for (const insight of insights) { + const oldInsightId = insight.id; + + const newInsight = await tryber.tables.UxCampaignInsights.do() + .insert({ + ...insight, + id: undefined, + campaign_id: cpIdExperiential, + severity_id: tagsMap.get(insight.severity_id), + }) + .returning("id"); + + const newInsightId = newInsight[0].id ?? newInsight[0]; + insightsMap.set(oldInsightId, newInsightId); + } + + // Link insights to observations + const insightObservations = + await tryber.tables.UxCampaignInsightsToObservations.do() + .select() + .whereIn( + "insight_id", + insights.map((insight) => insight.id) + ); + + if (insightObservations.length) { + for (const insightObservation of insightObservations) { + await tryber.tables.UxCampaignInsightsToObservations.do().insert({ + ...insightObservation, + id: undefined, + insight_id: insightsMap.get(insightObservation.insight_id), + observation_id: observationMap.get(insightObservation.observation_id), + }); + } + } + } +} + +export { createCpExperiential }; diff --git a/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.spec.ts new file mode 100644 index 000000000..511a26f30 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.spec.ts @@ -0,0 +1,333 @@ +import { createCpFunctional } from "."; +import { tryber } from "@src/features/database"; + +describe("createCpFunctional", () => { + const targetCampaignId = 69; + const targetProjectId = 6969; + + beforeAll(async () => { + await tryber.tables.WpAppqEvdCampaign.do().insert({ + id: targetCampaignId, + title: "Title - Functional Campaign", + description: "Functional Campaign", + campaign_type: 1, + campaign_type_id: 1, + project_id: 6969, + platform_id: 1, + start_date: "2021-01-01", + end_date: "2021-01-01", + page_preview_id: 1, + page_manual_id: 1, + pm_id: 1, + customer_id: 1, + customer_title: "Customer Title - Functional Campaign", + }); + await tryber.tables.WpAppqCampaignTask.do().insert([ + { + id: 10, + campaign_id: targetCampaignId, + title: "Title - Campaign Task", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + { + id: 20, + campaign_id: 69, + title: "Title - Campaign Task", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + { + id: 999, + campaign_id: 9999, + title: "Title - Campaign Task other cp", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + ]); + await tryber.tables.WpAppqCampaignTaskGroup.do().insert([ + { + task_id: 10, + group_id: 1, + }, + { + task_id: 20, + group_id: 0, + }, + { + task_id: 999, + group_id: 0, + }, + ]); + await tryber.tables.WpAppqUserTask.do().insert([ + { + id: 100, + task_id: 10, + tester_id: 11111, + is_completed: 1, + }, + { + id: 101, + task_id: 10, + tester_id: 32, + is_completed: 0, + }, + { + id: 102, + task_id: 999, + tester_id: 32, + is_completed: 1, + }, + ]); + await tryber.tables.WpCrowdAppqHasCandidate.do().insert([ + { + user_id: 11111, + campaign_id: targetCampaignId, + }, + { + user_id: 32, + campaign_id: targetCampaignId, + }, + { + user_id: 32, + campaign_id: 9999, + }, + ]); + await tryber.tables.WpAppqEvdBug.do().insert([ + { + id: 1, + campaign_id: targetCampaignId, + wp_user_id: 11111, + reviewer: 32, + last_editor_id: 11111, + }, + { + id: 2, + campaign_id: targetCampaignId, + wp_user_id: 32, + reviewer: 11111, + last_editor_id: 11111, + }, + { + id: 999, + campaign_id: 9999, + wp_user_id: 11111, + reviewer: 32, + last_editor_id: 11111, + }, + ]); + await tryber.tables.WpAppqEvdBugMedia.do().insert([ + { + id: 10, + bug_id: 1, + }, + { + id: 20, + bug_id: 2, + }, + { + id: 999, + bug_id: 999, + }, + ]); + }); + afterAll(async () => { + await tryber.tables.WpAppqEvdCampaign.do().delete(); + await tryber.tables.WpAppqCampaignTask.do().delete(); + await tryber.tables.WpAppqCampaignTaskGroup.do().delete(); + await tryber.tables.WpAppqUserTask.do().delete(); + await tryber.tables.WpCrowdAppqHasCandidate.do().delete(); + await tryber.tables.WpAppqEvdBug.do().delete(); + await tryber.tables.WpAppqEvdBugMedia.do().delete(); + }); + + it("Should insert one row in wp_appq_evd_campaigns", async () => { + const campaignBefore = await tryber.tables.WpAppqEvdCampaign.do().select(); + + await createCpFunctional({ + projectId: targetProjectId, + sourceCpId: targetCampaignId, + }); + const campaignAfter = await tryber.tables.WpAppqEvdCampaign.do().select(); + expect(campaignAfter.length).toBe(campaignBefore.length + 1); + }); + it("Should insert the functional campaign with same data the target campaign except projectId", async () => { + const campaign = await tryber.tables.WpAppqEvdCampaign.do() + .select() + .where("id", targetCampaignId) + .first(); + + const { cpIdFunctional: newCampaignId } = await createCpFunctional({ + projectId: targetCampaignId, + sourceCpId: targetCampaignId, + }); + + const newCampaign = await tryber.tables.WpAppqEvdCampaign.do() + .select() + .where("id", newCampaignId) + .first(); + + expect(newCampaign).toMatchObject({ + title: campaign?.title, + description: campaign?.description, + campaign_type: campaign?.campaign_type, + campaign_type_id: campaign?.campaign_type_id, + platform_id: campaign?.platform_id, + start_date: campaign?.start_date, + end_date: campaign?.end_date, + page_preview_id: campaign?.page_preview_id, + page_manual_id: campaign?.page_manual_id, + pm_id: campaign?.pm_id, + customer_id: campaign?.customer_id, + customer_title: campaign?.customer_title, + }); + }); + it("Should insert the functional campaign with target projectId", async () => { + const { cpIdFunctional: newCampaignId } = await createCpFunctional({ + projectId: targetProjectId, + sourceCpId: targetCampaignId, + }); + + const newCampaign = await tryber.tables.WpAppqEvdCampaign.do() + .select() + .where("id", newCampaignId) + .first(); + + expect(newCampaign).toMatchObject({ + project_id: targetProjectId, + }); + }); + it("Should insert the same number of candidates of target-campaign candidates", async () => { + const targetCandidates = await tryber.tables.WpCrowdAppqHasCandidate.do() + .select() + .where("campaign_id", targetCampaignId); + + const { cpIdFunctional: newCampaignId } = await createCpFunctional({ + projectId: targetProjectId, + sourceCpId: targetCampaignId, + }); + + const newCandidates = await tryber.tables.WpCrowdAppqHasCandidate.do() + .select() + .where("campaign_id", newCampaignId); + expect(newCandidates.length).toEqual(targetCandidates.length); + }); + it("Should insert the same number of usecases of target-usecases", async () => { + const targetTasks = await tryber.tables.WpAppqCampaignTask.do() + .select() + .where("campaign_id", targetCampaignId); + + const { cpIdFunctional: newCampaignId } = await createCpFunctional({ + projectId: targetProjectId, + sourceCpId: targetCampaignId, + }); + + const newTasks = await tryber.tables.WpAppqCampaignTask.do() + .select() + .where("campaign_id", newCampaignId); + expect(newTasks.length).toEqual(targetTasks.length); + }); + // it("Should insert the same number of task-groups of target-campaign task-groups", async () => { + // const targetTaskGroups = await tryber.tables.WpAppqCampaignTaskGroup.do() + // .select() + // .where("campaign_id", targetCampaignId); + + // const { cpIdFunctional: newCampaignId } = await createCpFunctional({ + // projectId: targetProjectId, + // sourceCpId: targetCampaignId, + // }); + + // const newTaskGroups = await tryber.tables.WpAppqCampaignTaskGroup.do() + // .select() + // .where("campaign_id", newCampaignId); + // expect(newTaskGroups.length).toEqual(targetTaskGroups.length); + // }); + it("Should insert the same number of user-tasks of target-campaign user-tasks", async () => { + const targetTaskIds = await tryber.tables.WpAppqCampaignTask.do() + .select("id") + .where("campaign_id", targetCampaignId); + + const targetUserTasks = await tryber.tables.WpAppqUserTask.do() + .select() + .whereIn( + "task_id", + targetTaskIds.map((task) => task.id) + ); + + const { cpIdFunctional: newCampaignId } = await createCpFunctional({ + projectId: targetProjectId, + sourceCpId: targetCampaignId, + }); + + const newTaskIds = await tryber.tables.WpAppqCampaignTask.do() + .select("id") + .where("campaign_id", newCampaignId); + + const newUserTasks = await tryber.tables.WpAppqUserTask.do() + .select() + .whereIn( + "task_id", + newTaskIds.map((task) => task.id) + ); + expect(newUserTasks.length).toEqual(targetUserTasks.length); + }); + it("Should insert the same number of bugs of target-campaign bugs", async () => { + const targetBugs = await tryber.tables.WpAppqEvdBug.do() + .select() + .where("campaign_id", targetCampaignId); + + const { cpIdFunctional: newCampaignId } = await createCpFunctional({ + projectId: targetProjectId, + sourceCpId: targetCampaignId, + }); + + const newBugs = await tryber.tables.WpAppqEvdBug.do() + .select() + .where("campaign_id", newCampaignId); + expect(newBugs.length).toEqual(targetBugs.length); + }); + it("Should insert the same number of bug-medias of target-campaign bug-medias", async () => { + const targetBugIds = await tryber.tables.WpAppqEvdBug.do() + .select("id") + .where("campaign_id", targetCampaignId); + + const targetBugMedias = await tryber.tables.WpAppqEvdBugMedia.do() + .select() + .whereIn( + "bug_id", + targetBugIds.map((bug) => bug.id) + ); + + const { cpIdFunctional: newCampaignId } = await createCpFunctional({ + projectId: targetProjectId, + sourceCpId: targetCampaignId, + }); + + const newBugIds = await tryber.tables.WpAppqEvdBug.do() + .select("id") + .where("campaign_id", newCampaignId); + + const newBugMedias = await tryber.tables.WpAppqEvdBugMedia.do() + .select() + .whereIn( + "bug_id", + newBugIds.map((bug) => bug.id) + ); + expect(newBugMedias.length).toEqual(targetBugMedias.length); + }); +}); diff --git a/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.ts b/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.ts new file mode 100644 index 000000000..9c1a9d9b1 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.ts @@ -0,0 +1,119 @@ +import { tryber } from "@src/features/database"; +import { getCpData } from "../getCpData"; + +async function createCpFunctional({ + projectId, + sourceCpId, +}: { + projectId: number; + sourceCpId: number; +}): Promise<{ cpIdFunctional: number }> { + const data = await getCpData({ cpId: sourceCpId }); + + if (!data.campaign) return { cpIdFunctional: 0 }; + + const oldCpId = data.campaign.id; + data.campaign.id = undefined; + + const cpIdFunctional = await insertCampaign(); + + data.campaign.id = cpIdFunctional; + data.campaign.oldCpId = oldCpId; + + if (cpIdFunctional) { + await insertCandidates(); + + if (data.usecases) { + for (const usecase of data.usecases) { + const { oldUsecaseId, newUsecaseId } = await insertUsecase(usecase); + + await insertUserTasks(oldUsecaseId, newUsecaseId); + } + } + + await insertBugs(); + } + + return { cpIdFunctional }; + + async function insertCampaign() { + const newCampaign = await tryber.tables.WpAppqEvdCampaign.do() + .insert({ ...data.campaign, project_id: projectId }) + .returning("id"); + const cpIdFunctional = newCampaign[0].id ?? newCampaign[0]; + return cpIdFunctional ?? 0; + } + + async function insertBugs() { + if (data.bugs) { + for (const bug of data.bugs) { + bug.campaign_id = cpIdFunctional; + const oldBugId = bug.id; + bug.id = undefined; + const usecaseRef = data.usecases?.find( + (usecase) => usecase.oldUsecaseId === bug.application_section_id + ); + bug.application_section = usecaseRef?.title; + bug.application_section_id = usecaseRef?.id; + const newBug = await tryber.tables.WpAppqEvdBug.do() + .insert(bug) + .returning("id"); + const newBugId = newBug[0].id ?? newBug[0]; + bug.id = newBugId; + bug.oldBugId = oldBugId; + + if (data.bugMedias) { + for (const bugMedia of data.bugMedias) { + if (bugMedia.bug_id === bug.oldBugId) { + const oldBugMediaId = bugMedia.id; + bugMedia.bug_id = newBugId; + bugMedia.id = undefined; + const newBugMedia = await tryber.tables.WpAppqEvdBugMedia.do() + .insert(bugMedia) + .returning("id"); + bugMedia.oldBugMediaId = oldBugMediaId; + bugMedia.id = newBugMedia[0].id ?? newBugMedia[0]; + } + } + } + } + } + } + + async function insertUsecase(usecase: any) { + usecase.campaign_id = cpIdFunctional; + const oldUsecaseId = usecase.id; + usecase.id = undefined; + const newUsecase = await tryber.tables.WpAppqCampaignTask.do() + .insert(usecase) + .returning("id"); + const newUsecaseId = newUsecase[0].id ?? newUsecase[0]; + usecase.id = newUsecaseId; + usecase.campaign_id = cpIdFunctional; + usecase.oldUsecaseId = oldUsecaseId; + return { oldUsecaseId, newUsecaseId }; + } + + async function insertUserTasks(oldUsecaseId: any, newUsecaseId: number) { + if (data.userTasks) { + for (const userTask of data.userTasks) { + if (userTask.task_id === oldUsecaseId) { + userTask.task_id = newUsecaseId; + userTask.id = undefined; + await tryber.tables.WpAppqUserTask.do().insert(userTask); + } + } + } + } + + async function insertCandidates() { + if (data.candidates) { + for (const candidate of data.candidates) { + candidate.campaign_id = cpIdFunctional; + await tryber.tables.WpCrowdAppqHasCandidate.do().insert(candidate); + } + } + } +} + +export { createCpFunctional }; diff --git a/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.spec.ts new file mode 100644 index 000000000..71745e4e9 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.spec.ts @@ -0,0 +1,53 @@ +import { createDemoProject } from "."; +import { tryber } from "@src/features/database"; + +describe("createDemoProject", () => { + afterEach(async () => { + await tryber.tables.WpAppqProject.do().delete(); + }); + + it("Should insert the demo project", async () => { + const workspaceId = 69; + + const projectsBefore = await tryber.tables.WpAppqProject.do() + .select() + .where({ display_name: "Demo Project" }); + + expect(projectsBefore.length).toBe(0); + const { projectId } = await createDemoProject({ workspaceId }); + + const progetsAfter = await tryber.tables.WpAppqProject.do().select(); + + expect(progetsAfter.length).toBe(1); + expect(progetsAfter[0]).toMatchObject({ + id: projectId, + display_name: "Demo Project", + customer_id: workspaceId, + edited_by: 0, + }); + }); + it("Should return the id", async () => { + const workspaceId = 69; + const projectsBefore = await tryber.tables.WpAppqProject.do().select(); + + expect(projectsBefore.length).toBe(0); + const { projectId } = await createDemoProject({ workspaceId }); + + const progetsAfter = await tryber.tables.WpAppqProject.do().select(); + + expect(progetsAfter[0].id).toBe(projectId); + }); + + it("Should insert the project releated to workspace", async () => { + const workspaceId = 69; + const projectsbefore = await tryber.tables.WpAppqProject.do().select(); + expect(projectsbefore.length).toBe(0); + + const { projectId } = await createDemoProject({ workspaceId }); + const projects = await tryber.tables.WpAppqProject.do() + .select() + .where("id", projectId); + + expect(projects.length).toBe(1); + }); +}); diff --git a/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.ts b/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.ts new file mode 100644 index 000000000..93d71fbed --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.ts @@ -0,0 +1,20 @@ +import { tryber } from "@src/features/database"; + +async function createDemoProject({ + workspaceId, +}: { + workspaceId: number; +}): Promise<{ projectId: number }> { + const project = await tryber.tables.WpAppqProject.do() + .insert({ + display_name: "Demo Project", + customer_id: workspaceId, + edited_by: 0, + }) + .returning("id"); + const projectId = project[0].id ?? project[0]; + + return { projectId }; +} + +export { createDemoProject }; diff --git a/src/routes/customers/_post/createDemoEnvironment/getCpData/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/getCpData/index.spec.ts new file mode 100644 index 000000000..b6ccc3d47 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/getCpData/index.spec.ts @@ -0,0 +1,315 @@ +import { getCpData } from "."; +import { tryber } from "@src/features/database"; + +describe("getCpData", () => { + beforeAll(async () => { + await tryber.tables.WpAppqEvdCampaign.do().insert({ + id: 69, + title: "Title - Functional Campaign", + description: "Functional Campaign", + campaign_type: 1, + campaign_type_id: 1, + project_id: 6969, + platform_id: 1, + start_date: "2021-01-01", + end_date: "2021-01-01", + page_preview_id: 1, + page_manual_id: 1, + pm_id: 1, + customer_id: 1, + customer_title: "Customer Title - Functional Campaign", + }); + await tryber.tables.WpAppqCampaignTask.do().insert([ + { + id: 10, + campaign_id: 69, + title: "Title - Campaign Task", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + { + id: 20, + campaign_id: 69, + title: "Title - Campaign Task", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + { + id: 999, + campaign_id: 9999, + title: "Title - Campaign Task other cp", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + ]); + await tryber.tables.WpAppqCampaignTaskGroup.do().insert([ + { + task_id: 10, + group_id: 1, + }, + { + task_id: 20, + group_id: 0, + }, + { + task_id: 999, + group_id: 0, + }, + ]); + await tryber.tables.WpAppqUserTask.do().insert([ + { + id: 100, + task_id: 10, + tester_id: 11111, + is_completed: 1, + }, + { + id: 101, + task_id: 10, + tester_id: 32, + is_completed: 0, + }, + { + id: 102, + task_id: 999, + tester_id: 32, + is_completed: 1, + }, + ]); + await tryber.tables.WpCrowdAppqHasCandidate.do().insert([ + { + user_id: 11111, + campaign_id: 69, + }, + { + user_id: 32, + campaign_id: 69, + }, + { + user_id: 32, + campaign_id: 9999, + }, + ]); + await tryber.tables.WpAppqEvdBug.do().insert([ + { + id: 1, + campaign_id: 69, + wp_user_id: 11111, + reviewer: 32, + last_editor_id: 11111, + }, + { + id: 2, + campaign_id: 69, + wp_user_id: 32, + reviewer: 11111, + last_editor_id: 11111, + }, + { + id: 999, + campaign_id: 9999, + wp_user_id: 11111, + reviewer: 32, + last_editor_id: 11111, + }, + ]); + await tryber.tables.WpAppqEvdBugMedia.do().insert([ + { + id: 10, + bug_id: 1, + }, + { + id: 20, + bug_id: 2, + }, + { + id: 999, + bug_id: 999, + }, + ]); + }); + afterAll(async () => { + await tryber.tables.WpAppqEvdCampaign.do().delete(); + await tryber.tables.WpAppqCampaignTask.do().delete(); + await tryber.tables.WpAppqCampaignTaskGroup.do().delete(); + await tryber.tables.WpAppqUserTask.do().delete(); + await tryber.tables.WpCrowdAppqHasCandidate.do().delete(); + await tryber.tables.WpAppqEvdBug.do().delete(); + await tryber.tables.WpAppqEvdBugMedia.do().delete(); + }); + + it("Should return empty object if campaign does not exist", async () => { + const cpId = 9999; + const campaigns = await tryber.tables.WpAppqEvdCampaign.do() + .select() + .where("id", cpId); + expect(campaigns.length).toBe(0); + const cpData = await getCpData({ cpId }); + + expect(cpData).toBeInstanceOf(Object); + expect(cpData).toMatchObject({}); + }); + + it("Should return campaign data", async () => { + const cpId = 69; + const campaigns = await tryber.tables.WpAppqEvdCampaign.do() + .select() + .where("id", cpId); + expect(campaigns.length).toBe(1); + const data = await getCpData({ cpId }); + + expect(data).toHaveProperty("campaign"); + expect(data.campaign).toMatchObject({ + id: 69, + title: "Title - Functional Campaign", + description: "Functional Campaign", + campaign_type: 1, + campaign_type_id: 1, + project_id: 6969, + platform_id: 1, + start_date: "2021-01-01", + end_date: "2021-01-01", + page_preview_id: 1, + page_manual_id: 1, + pm_id: 1, + customer_id: 1, + customer_title: "Customer Title - Functional Campaign", + }); + }); + + it("Should return campaign usecases if campagin has usecases", async () => { + const cpId = 69; + const data = await getCpData({ cpId }); + + expect(data).toHaveProperty("usecases"); + expect(data.usecases).toHaveLength(2); + expect(data.usecases).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 10, + campaign_id: 69, + title: "Title - Campaign Task", + }), + expect.objectContaining({ + id: 20, + campaign_id: 69, + title: "Title - Campaign Task", + }), + ]) + ); + }); + it("Should return campaign usecaseGroups if campagin has group of usecases", async () => { + const cpId = 69; + const data = await getCpData({ cpId }); + + expect(data).toHaveProperty("usecaseGroups"); + expect(data.usecaseGroups).toHaveLength(2); + expect(data.usecaseGroups).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + task_id: 10, + group_id: 1, + }), + expect.objectContaining({ + task_id: 20, + group_id: 0, + }), + ]) + ); + }); + it("Should return campaign userTask if campagin has user tasks", async () => { + const cpId = 69; + const data = await getCpData({ cpId }); + expect(data).toHaveProperty("userTasks"); + expect(data.userTasks).toHaveLength(2); + expect(data.userTasks).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 100, + task_id: 10, + tester_id: 11111, + }), + expect.objectContaining({ + id: 101, + task_id: 10, + tester_id: 32, + }), + ]) + ); + }); + it("Should return campaign candidates if campagin has candidates", async () => { + const cpId = 69; + const data = await getCpData({ cpId }); + expect(data).toHaveProperty("candidates"); + expect(data.candidates).toHaveLength(2); + expect(data.candidates).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + user_id: 11111, + campaign_id: 69, + }), + expect.objectContaining({ + user_id: 32, + campaign_id: 69, + }), + ]) + ); + }); + it("Should return campaign bugs if campaign has bugs", async () => { + const cpId = 69; + const data = await getCpData({ cpId }); + expect(data).toHaveProperty("bugs"); + expect(data.bugs).toHaveLength(2); + expect(data.bugs).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 1, + campaign_id: 69, + wp_user_id: 11111, + reviewer: 32, + last_editor_id: 11111, + }), + expect.objectContaining({ + id: 2, + campaign_id: 69, + wp_user_id: 32, + reviewer: 11111, + last_editor_id: 11111, + }), + ]) + ); + }); + it("Should return bugMedias if campaign has bugMedias", async () => { + const cpId = 69; + const data = await getCpData({ cpId }); + expect(data).toHaveProperty("bugMedias"); + expect(data.bugMedias).toHaveLength(2); + expect(data.bugMedias).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 10, + bug_id: 1, + }), + expect.objectContaining({ + id: 20, + bug_id: 2, + }), + ]) + ); + }); +}); diff --git a/src/routes/customers/_post/createDemoEnvironment/getCpData/index.ts b/src/routes/customers/_post/createDemoEnvironment/getCpData/index.ts new file mode 100644 index 000000000..b06ab9361 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/getCpData/index.ts @@ -0,0 +1,78 @@ +import { tryber } from "@src/features/database"; + +interface CampaignData { + campaign?: any; + usecases?: any[]; + usecaseGroups?: any[]; + userTasks?: any[]; + candidates?: any[]; + bugs?: any[]; + bugMedias?: any[]; +} + +async function getCpData({ cpId }: { cpId: number }): Promise { + const res: CampaignData = {}; + const cpData = await tryber.tables.WpAppqEvdCampaign.do() + .select() + .where("id", cpId) + .first(); + if (cpData) { + res.campaign = cpData; + } + + const usecases = await tryber.tables.WpAppqCampaignTask.do() + .select() + .where("campaign_id", cpId); + if (usecases.length > 0) { + res.usecases = usecases; + + const usecaseGroups = await tryber.tables.WpAppqCampaignTaskGroup.do() + .select() + .whereIn( + "task_id", + usecases.map((usecase) => usecase.id) + ); + if (usecaseGroups.length > 0) { + res.usecaseGroups = usecaseGroups; + } + + const userTasks = await tryber.tables.WpAppqUserTask.do() + .select() + .whereIn( + "task_id", + usecases.map((usecase) => usecase.id) + ); + if (userTasks.length > 0) { + res.userTasks = userTasks; + } + } + + const candidates = await tryber.tables.WpCrowdAppqHasCandidate.do() + .select() + .where("campaign_id", cpId); + if (candidates.length > 0) { + res.candidates = candidates; + } + + const bugs = await tryber.tables.WpAppqEvdBug.do() + .select() + .where("campaign_id", cpId); + + if (bugs.length > 0) { + res.bugs = bugs; + + const bugMedias = await tryber.tables.WpAppqEvdBugMedia.do() + .select() + .whereIn( + "bug_id", + bugs.map((bug) => bug.id) + ); + if (bugMedias.length > 0) { + res.bugMedias = bugMedias; + } + } + + return res; +} + +export { getCpData }; diff --git a/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.spec.ts new file mode 100644 index 000000000..684d4b2d6 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.spec.ts @@ -0,0 +1,408 @@ +import { getExperientialData } from "."; +import { tryber } from "@src/features/database"; +const targetCpId = 69; +const transcript_1 = `{ + "speakers": 2, + "paragraphs": [ + { + "end": 0.65999997, + "text": "Bene?", + "start": 0.16, + "words": [ + { + "end": 0.65999997, + "word": "Bene?", + "start": 0.16, + "speaker": 0 + } + ], + "speaker": 0 + }, { + "end": 2.6599998, + "text": "Sì la vedo la sento bene", + "start": 0.88, + "words": [ + { + "end": 1.1999999, + "word": "Sì", + "start": 0.88, + "speaker": 1 + }, { + "end": 1.4399999, + "word": "la", + "start": 1.1999999, + "speaker": 1 + }, { + "end": 1.5999999, + "word": "vedo", + "start": 1.4399999, + "speaker": 1 + }, { + "end": 1.8399999, + "word": "la", + "start": 1.5999999, + "speaker": 1 + } + ] + } + ] +}`; +describe("getExperientialData", () => { + beforeAll(async () => { + await tryber.tables.WpAppqEvdCampaign.do().insert({ + id: 69, + title: "Title - Functional Campaign", + description: "Functional Campaign", + campaign_type: 1, + campaign_type_id: 1, + project_id: 6969, + platform_id: 1, + start_date: "2021-01-01", + end_date: "2021-01-01", + page_preview_id: 1, + page_manual_id: 1, + pm_id: 1, + customer_id: 1, + customer_title: "Customer Title - Functional Campaign", + }); + await tryber.tables.WpAppqCampaignTask.do().insert([ + { + id: 10, + campaign_id: 69, + title: "Title - Campaign Task", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + { + id: 20, + campaign_id: 69, + title: "Title - Campaign Task", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + { + id: 999, + campaign_id: 9999, + title: "Title - Campaign Task other cp", + content: "Functional Campaign", + jf_code: "jf_code", + jf_text: "jf_text", + is_required: 1, + simple_title: "simple_title", + info: "info", + prefix: "prefix", + }, + ]); + await tryber.tables.WpAppqUserTaskMedia.do().insert([ + { + id: 10, + campaign_task_id: 10, + user_task_id: 0, + tester_id: 11111, + location: "location", + }, + { + id: 20, + campaign_task_id: 20, + user_task_id: 0, + tester_id: 11111, + location: "location", + }, + { + id: 999, + campaign_task_id: 999, + user_task_id: 0, + tester_id: 32, + location: "location", + }, + ]); + await tryber.tables.WpAppqUsecaseMediaObservations.do().insert([ + { + id: 10, + media_id: 10, + video_ts: 10, + video_ts_end: 20, + name: "name", + description: "description", + ux_note: "ux_note", + }, + { + id: 20, + media_id: 20, + video_ts: 10, + video_ts_end: 30, + name: "name", + description: "description", + ux_note: "ux_note", + }, + { + id: 999, + media_id: 999, + video_ts: 10, + video_ts_end: 30, + name: "name", + description: "description", + ux_note: "ux_note", + }, + ]); + await tryber.tables.WpAppqUsecaseMediaTagType.do().insert([ + { + id: 10, + campaign_id: targetCpId, + name: "tagGroup10", + }, + { + id: 20, + campaign_id: targetCpId, + name: "tagGroup20", + }, + { + id: 999, + campaign_id: 99999, + name: "tagGroup999", + }, + ]); + await tryber.tables.WpAppqUsecaseMediaObservationsTags.do().insert([ + { id: 10, type: 10, name: "tagName10", style: "white" }, + { id: 20, type: 10, name: "tagName20", style: "red" }, + { id: 30, type: 20, name: "tagName30", style: "green" }, + { id: 999, type: 999, name: "tagName999", style: "blue" }, + ]); + await tryber.tables.WpAppqUsecaseMediaObservationsTagsLink.do().insert([ + { id: 10, tag_id: 10, observation_id: 10 }, + { id: 20, tag_id: 20, observation_id: 20 }, + { id: 30, tag_id: 30, observation_id: 20 }, + { id: 999, tag_id: 999, observation_id: 999 }, + ]); + await tryber.tables.MediaTranscripts.do().insert([ + { + id: 11, + media_id: 10, + transcript: transcript_1, + language: "it", + }, + { + id: 22, + media_id: 20, + transcript: transcript_1, + language: "en", + }, + { + id: 999, + media_id: 999, + transcript: transcript_1, + language: "zh", + }, + ]); + await tryber.tables.MediaTranscriptsTranslations.do().insert([ + { id: 11, media_id: 10, translation: "translation", language: "en" }, + { id: 22, media_id: 20, translation: "translation", language: "it" }, + { id: 999, media_id: 999, translation: "translation", language: "it" }, + ]); + }); + afterAll(async () => { + await tryber.tables.WpAppqEvdCampaign.do().delete(); + await tryber.tables.WpAppqCampaignTask.do().delete(); + await tryber.tables.WpAppqUserTaskMedia.do().delete(); + await tryber.tables.WpAppqUsecaseMediaObservations.do().delete(); + await tryber.tables.WpAppqUsecaseMediaTagType.do().delete(); + await tryber.tables.WpAppqUsecaseMediaObservationsTags.do().delete(); + await tryber.tables.WpAppqUsecaseMediaObservationsTagsLink.do().delete(); + await tryber.tables.MediaTranscripts.do().delete(); + await tryber.tables.MediaTranscriptsTranslations.do().delete(); + }); + + it("Should return empty object if campaign does not exist", async () => { + const cpId = 9999; + const campaigns = await tryber.tables.WpAppqEvdCampaign.do() + .select() + .where("id", cpId); + expect(campaigns.length).toBe(0); + const cpData = await getExperientialData({ cpId }); + + expect(cpData).toBeInstanceOf(Object); + expect(cpData).toMatchObject({}); + }); + + it("Should return userTasksMedia if campagin has usreTasksMedia", async () => { + const data = await getExperientialData({ cpId: targetCpId }); + + expect(data).toHaveProperty("userTasksMedia"); + expect(data.userTasksMedia).toHaveLength(2); + expect(data.userTasksMedia).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 10, + tester_id: 11111, + campaign_task_id: 10, + user_task_id: 0, + }), + expect.objectContaining({ + id: 20, + tester_id: 11111, + campaign_task_id: 20, + user_task_id: 0, + }), + ]) + ); + }); + it("Should return campaign observation if campagin has observation", async () => { + const data = await getExperientialData({ cpId: targetCpId }); + + expect(data).toHaveProperty("observations"); + expect(data.observations).toHaveLength(2); + expect(data.observations).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 10, + media_id: 10, + video_ts: 10, + video_ts_end: 20, + name: "name", + description: "description", + ux_note: "ux_note", + }), + expect.objectContaining({ + id: 20, + media_id: 20, + video_ts: 10, + video_ts_end: 30, + name: "name", + description: "description", + ux_note: "ux_note", + }), + ]) + ); + }); + it("Should return all campaign tags", async () => { + const data = await getExperientialData({ cpId: targetCpId }); + + expect(data).toHaveProperty("tags"); + expect(data.tags).toHaveLength(3); + expect(data.tags).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 10, + type: 10, + name: "tagName10", + style: "white", + created_at: expect.any(String), + updated_at: expect.any(String), + }), + expect.objectContaining({ + id: 20, + type: 10, + name: "tagName20", + style: "red", + created_at: expect.any(String), + updated_at: expect.any(String), + }), + expect.objectContaining({ + id: 30, + type: 20, + name: "tagName30", + style: "green", + created_at: expect.any(String), + updated_at: expect.any(String), + }), + ]) + ); + }); + it("Should return all campaign task groups", async () => { + const data = await getExperientialData({ cpId: targetCpId }); + + expect(data).toHaveProperty("tagTypes"); + expect(data.tagTypes).toHaveLength(2); + expect(data.tagTypes).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 10, + campaign_id: targetCpId, + name: "tagGroup10", + }), + expect.objectContaining({ + id: 20, + campaign_id: targetCpId, + name: "tagGroup20", + }), + ]) + ); + }); + it("Should return all tagsLinks with observations", async () => { + const data = await getExperientialData({ cpId: targetCpId }); + + expect(data).toHaveProperty("tagLinks"); + expect(data.tagLinks).toHaveLength(3); + expect(data.tagLinks).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 10, + tag_id: 10, + observation_id: 10, + }), + expect.objectContaining({ + id: 20, + tag_id: 20, + observation_id: 20, + }), + expect.objectContaining({ + id: 30, + tag_id: 30, + observation_id: 20, + }), + ]) + ); + }); + it("Should return all campaign transcripts", async () => { + const data = await getExperientialData({ cpId: targetCpId }); + + expect(data).toHaveProperty("transcripts"); + expect(data.transcripts).toHaveLength(2); + expect(data.transcripts).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 11, + media_id: 10, + transcript: transcript_1, + }), + expect.objectContaining({ + id: 22, + media_id: 20, + transcript: transcript_1, + }), + ]) + ); + }); + it("Should return all campaign transcript translations", async () => { + const data = await getExperientialData({ cpId: targetCpId }); + + expect(data).toHaveProperty("translations"); + expect(data.translations).toHaveLength(2); + expect(data.translations).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: 11, + media_id: 10, + translation: "translation", + language: "en", + }), + expect.objectContaining({ + id: 22, + media_id: 20, + translation: "translation", + language: "it", + }), + ]) + ); + }); +}); diff --git a/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.ts b/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.ts new file mode 100644 index 000000000..4eb988f7a --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.ts @@ -0,0 +1,120 @@ +import { tryber } from "@src/features/database"; + +interface TaggingToolData { + userTasksMedia?: any[]; + observations?: any[]; + tags?: any[]; + tagTypes?: any[]; + tagLinks?: any[]; + transcripts?: any[]; + translations?: any[]; +} + +async function getExperientialData({ + cpId, +}: { + cpId: number; +}): Promise { + const res: TaggingToolData = {}; + + // Campaign tasks + const campaignTasksIds = await tryber.tables.WpAppqCampaignTask.do() + .select("id") + .where("campaign_id", cpId); + + // User tasks media + const videos = await tryber.tables.WpAppqUserTaskMedia.do() + .select() + .whereIn( + "campaign_task_id", + campaignTasksIds.map((ct) => ct.id) + ); + + if (videos.length > 0) { + res.userTasksMedia = videos; + + // Media Observations + const observations = await tryber.tables.WpAppqUsecaseMediaObservations.do() + .select("wp_appq_usecase_media_observations.*") + .join( + "wp_appq_user_task_media", + "wp_appq_usecase_media_observations.media_id", + "wp_appq_user_task_media.id" + ) + .join( + "wp_appq_campaign_task", + "wp_appq_user_task_media.campaign_task_id", + "wp_appq_campaign_task.id" + ) + .where("wp_appq_campaign_task.campaign_id", cpId) + .groupBy("wp_appq_usecase_media_observations.id"); + + if (observations.length > 0) { + res.observations = observations; + } + + // Tag groups + const tagTypes = await tryber.tables.WpAppqUsecaseMediaTagType.do() + .select() + .where("campaign_id", cpId); + + if (tagTypes.length > 0) { + res.tagTypes = tagTypes; + } + + // Tags + const tags = await tryber.tables.WpAppqUsecaseMediaObservationsTags.do() + .select("wp_appq_usecase_media_observations_tags.*") + .join( + "wp_appq_usecase_media_tag_type", + "wp_appq_usecase_media_tag_type.id", + "wp_appq_usecase_media_observations_tags.type" + ) + .where("wp_appq_usecase_media_tag_type.campaign_id", cpId) + .groupBy("wp_appq_usecase_media_observations_tags.id"); + + if (tags.length > 0) { + res.tags = tags; + } + + // Tag links + const tagLinks = + await tryber.tables.WpAppqUsecaseMediaObservationsTagsLink.do() + .select("wp_appq_usecase_media_observations_tags_link.*") + .whereIn( + "tag_id", + tags.map((tag) => tag.id) + ) + .groupBy("wp_appq_usecase_media_observations_tags_link.id"); + + if (tagLinks.length > 0) { + res.tagLinks = tagLinks; + } + + // Transcripts + const transcripts = await tryber.tables.MediaTranscripts.do() + .select() + .whereIn( + "media_id", + videos.map((video) => video.id) + ); + if (transcripts.length > 0) { + res.transcripts = transcripts; + + // Translations + const translations = await tryber.tables.MediaTranscriptsTranslations.do() + .select() + .whereIn( + "media_id", + videos.map((video) => video.id) + ); + if (translations.length > 0) { + res.translations = translations; + } + } + } + + return res; +} + +export { getExperientialData }; diff --git a/src/routes/customers/_post/createDemoEnvironment/index.ts b/src/routes/customers/_post/createDemoEnvironment/index.ts new file mode 100644 index 000000000..7af953dc4 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/index.ts @@ -0,0 +1,49 @@ +import { createCpExperiential } from "./createCpExperiential"; +import { createCpFunctional } from "./createCpFunctional"; +import { createDemoProject } from "./createDemoProject"; +import { workspaceExist } from "./workspaceExist"; + +async function createDemoEnvironment({ workspaceId }: { workspaceId: number }) { + const sourceCpIdFunctional = 7916; + const sourceCpIdExperiential = 7961; + + if (await workspaceExist({ workspaceId })) { + const { projectId } = await createDemoProject({ workspaceId }); + if (!projectId) { + console.log("Error creating project"); + return; + } + console.log("Project created"); + + const { cpIdFunctional } = await createCpFunctional({ + projectId, + sourceCpId: sourceCpIdFunctional, + }); + if (!cpIdFunctional) { + console.log("Error creating Functional CP"); + return; + } + console.log( + "Functional CP created from campaignId: ", + sourceCpIdFunctional + ); + + const { cpIdExperiential } = await createCpExperiential({ + projectId, + sourceCpId: sourceCpIdExperiential, + }); + if (!cpIdExperiential) { + console.log("Error creating Experiential CP"); + return; + } + console.log( + "Experiential CP created from campaignId: ", + sourceCpIdExperiential + ); + } else { + console.log("Workspace not found"); + return; + } +} + +export { createDemoEnvironment }; diff --git a/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.spec.ts new file mode 100644 index 000000000..3caf1de53 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.spec.ts @@ -0,0 +1,25 @@ +import { workspaceExist } from "./index"; +import { tryber } from "../db/database"; +describe("workspaceExist", () => { + beforeAll(async () => { + await tryber.tables.WpAppqCustomer.do().insert([ + { + id: 10, + company: "Company Name", + pm_id: 11111, + }, + ]); + }); + afterAll(async () => { + await tryber.tables.WpAppqCustomer.do().delete(); + }); + + it("should return true if workspace exist", async () => { + const result = await workspaceExist({ workspaceId: 10 }); + expect(result).toBe(true); + }); + it("should return false if workspace does not exist", async () => { + const result = await workspaceExist({ workspaceId: 20 }); + expect(result).toBe(false); + }); +}); diff --git a/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.ts b/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.ts new file mode 100644 index 000000000..4887a65c5 --- /dev/null +++ b/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.ts @@ -0,0 +1,16 @@ +import { tryber } from "@src/features/database"; + +async function workspaceExist({ + workspaceId, +}: { + workspaceId: number; +}): Promise { + const workspace = await tryber.tables.WpAppqCustomer.do() + .select("id") + .first() + .where("id", workspaceId); + + return workspace ? true : false; +} + +export { workspaceExist }; diff --git a/src/routes/customers/_post/index.ts b/src/routes/customers/_post/index.ts index 7dccd76f4..7dd0b6bc0 100644 --- a/src/routes/customers/_post/index.ts +++ b/src/routes/customers/_post/index.ts @@ -3,6 +3,7 @@ import OpenapiError from "@src/features/OpenapiError"; import { tryber } from "@src/features/database"; import UserRoute from "@src/features/routes/UserRoute"; +import { createDemoEnvironment } from "./createDemoEnvironment"; class RouteItem extends UserRoute<{ response: StoplightOperations["post-customers"]["responses"]["200"]["content"]["application/json"]; @@ -27,6 +28,10 @@ class RouteItem extends UserRoute<{ protected async prepare(): Promise { const customer = await this.createCustomer(); + if (customer && customer.id) { + await createDemoEnvironment({ workspaceId: customer.id }); + } + return this.setSuccess(201, customer); } From a4657ee8093f49c17e6c0e597d381644a7f2de30 Mon Sep 17 00:00:00 2001 From: sinatragianpaolo Date: Tue, 26 Nov 2024 15:05:19 +0100 Subject: [PATCH 2/9] refactor: Remove unused code and files related to creating a demo environment --- .../copyUxData/index.spec.ts | 68 --- .../createDemoEnvironment/copyUxData/index.ts | 44 -- .../createCpExperiential/index.spec.ts | 543 ------------------ .../createCpExperiential/index.ts | 344 ----------- .../createCpFunctional/index.spec.ts | 333 ----------- .../createCpFunctional/index.ts | 119 ---- .../createDemoProject/index.spec.ts | 53 -- .../createDemoProject/index.ts | 20 - .../getCpData/index.spec.ts | 315 ---------- .../createDemoEnvironment/getCpData/index.ts | 78 --- .../getExperientialData/index.spec.ts | 408 ------------- .../getExperientialData/index.ts | 120 ---- .../_post/createDemoEnvironment/index.ts | 5 - .../workspaceExist/index.spec.ts | 25 - .../workspaceExist/index.ts | 16 - src/routes/customers/_post/index.ts | 8 +- 16 files changed, 4 insertions(+), 2495 deletions(-) delete mode 100644 src/routes/customers/_post/createDemoEnvironment/copyUxData/index.spec.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/copyUxData/index.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.spec.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.spec.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.spec.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/getCpData/index.spec.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/getCpData/index.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.spec.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.spec.ts delete mode 100644 src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.ts diff --git a/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.spec.ts deleted file mode 100644 index d96c1790b..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.spec.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { copyUxData } from "."; -import { tryber } from "@src/features/database"; - -describe("copyUxData", () => { - const sourceCampaignId = 69; - - beforeAll(async () => { - await tryber.tables.UxCampaignData.do().insert([ - { - id: 1, - campaign_id: sourceCampaignId, - published: 1, - methodology_type: "metodology1", - methodology_description: "methodology_description", - goal: "goal", - users: 15, - }, - { - id: 2, - campaign_id: 9999, - published: 1, - methodology_type: "metodology1", - methodology_description: "methodology_description", - goal: "goal", - users: 15, - }, - ]); - await tryber.tables.UxCampaignQuestions.do().insert([ - { id: 1, campaign_id: sourceCampaignId, question: "question1" }, - { id: 2, campaign_id: sourceCampaignId, question: "question2" }, - ]); - }); - afterAll(async () => { - await tryber.tables.UxCampaignData.do().delete(); - await tryber.tables.UxCampaignQuestions.do().delete(); - }); - it("Should copy the ux data from source to target campaign", async () => { - const targetCpId = 12345; - await copyUxData({ sourceCpId: sourceCampaignId, targetCpId: targetCpId }); - const columnUx = [ - "published", - "methodology_type", - "methodology_description", - "goal", - "users", - ]; - const sourceData = await tryber.tables.UxCampaignData.do() - .select(columnUx) - .where("campaign_id", sourceCampaignId); - const targetData = await tryber.tables.UxCampaignData.do() - .select(columnUx) - .where("campaign_id", targetCpId); - expect(targetData).toHaveLength(1); - expect(targetData).toMatchObject(sourceData); - }); - it("Should copy the questions from source to target campaign", async () => { - const targetCpId = 54321; - await copyUxData({ sourceCpId: sourceCampaignId, targetCpId: targetCpId }); - const sourceQuestions = await tryber.tables.UxCampaignQuestions.do() - .select("question") - .where("campaign_id", sourceCampaignId); - const targetQuestions = await tryber.tables.UxCampaignQuestions.do() - .select("question") - .where("campaign_id", targetCpId); - expect(targetQuestions).toHaveLength(sourceQuestions.length); - expect(targetQuestions).toMatchObject(sourceQuestions); - }); -}); diff --git a/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.ts b/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.ts deleted file mode 100644 index 32cde5e4a..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/copyUxData/index.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { tryber } from "@src/features/database"; - -// ux - UxCampaignData -// questions - UxCampaignQuestions -const copyUxData = async ({ - sourceCpId, - targetCpId, -}: { - sourceCpId: number; - targetCpId: number; -}) => { - const sourceData = await tryber.tables.UxCampaignData.do() - .select( - "published", - "methodology_type", - "methodology_description", - "goal", - "users" - ) - .where("campaign_id", sourceCpId) - .first(); - - if (sourceData) { - await tryber.tables.UxCampaignData.do().insert({ - ...sourceData, - campaign_id: targetCpId, - }); - } - - const sourceQuestions = await tryber.tables.UxCampaignQuestions.do() - .select("question") - .where("campaign_id", sourceCpId); - - if (sourceQuestions.length) { - await tryber.tables.UxCampaignQuestions.do().insert( - sourceQuestions.map((question) => ({ - ...question, - campaign_id: targetCpId, - })) - ); - } -}; - -export { copyUxData }; diff --git a/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.spec.ts deleted file mode 100644 index ced0ac4f4..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.spec.ts +++ /dev/null @@ -1,543 +0,0 @@ -import { createCpExperiential } from "."; -import { tryber } from "@src/features/database"; - -describe("createCpExperiential", () => { - const targetCampaignId = 69; - const targetPorjectId = 6969; - const transcript_1 = `{ - "speakers": 2, - "paragraphs": [ - { - "end": 0.65999997, - "text": "Bene?", - "start": 0.16, - "words": [ - { - "end": 0.65999997, - "word": "Bene?", - "start": 0.16, - "speaker": 0 - } - ], - "speaker": 0 - }, { - "end": 2.6599998, - "text": "Sì la vedo la sento bene", - "start": 0.88, - "words": [ - { - "end": 1.1999999, - "word": "Sì", - "start": 0.88, - "speaker": 1 - }, { - "end": 1.4399999, - "word": "la", - "start": 1.1999999, - "speaker": 1 - }, { - "end": 1.5999999, - "word": "vedo", - "start": 1.4399999, - "speaker": 1 - }, { - "end": 1.8399999, - "word": "la", - "start": 1.5999999, - "speaker": 1 - } - ] - } - ] - }`; - - beforeAll(async () => { - await tryber.tables.WpAppqEvdCampaign.do().insert({ - id: targetCampaignId, - title: "Title - Functional Campaign", - description: "Functional Campaign", - campaign_type: 1, - campaign_type_id: 1, - project_id: 6969, - platform_id: 1, - start_date: "2021-01-01", - end_date: "2021-01-01", - page_preview_id: 1, - page_manual_id: 1, - pm_id: 1, - customer_id: 1, - customer_title: "Customer Title - Functional Campaign", - }); - await tryber.tables.WpAppqCampaignTask.do().insert([ - { - id: 10, - campaign_id: targetCampaignId, - title: "Title - Campaign Task10", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - { - id: 20, - campaign_id: 70, - title: "Title - Campaign Task20", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - { - id: 999, - campaign_id: 9999, - title: "Title - Campaign Task other cp", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - ]); - await tryber.tables.WpAppqCampaignTaskGroup.do().insert([ - { - task_id: 10, - group_id: 1, - }, - { - task_id: 20, - group_id: 0, - }, - { - task_id: 999, - group_id: 0, - }, - ]); - await tryber.tables.WpAppqUserTask.do().insert([ - { - id: 100, - task_id: 10, - tester_id: 11111, - is_completed: 1, - }, - { - id: 101, - task_id: 10, - tester_id: 32, - is_completed: 0, - }, - { - id: 102, - task_id: 999, - tester_id: 32, - is_completed: 1, - }, - ]); - await tryber.tables.WpCrowdAppqHasCandidate.do().insert([ - { - user_id: 11111, - campaign_id: targetCampaignId, - }, - { - user_id: 32, - campaign_id: targetCampaignId, - }, - { - user_id: 32, - campaign_id: 9999, - }, - ]); - await tryber.tables.WpAppqUserTaskMedia.do().insert([ - { - id: 10, - campaign_task_id: 10, - user_task_id: 100, - tester_id: 11111, - location: "location10", - }, - { - id: 20, - campaign_task_id: 20, - user_task_id: 101, - tester_id: 11111, - location: "location20", - }, - { - id: 999, - campaign_task_id: 999, - user_task_id: 100, - tester_id: 32, - location: "location999", - }, - ]); - await tryber.tables.WpAppqUsecaseMediaObservations.do().insert([ - { - id: 10, - media_id: 10, - video_ts: 10, - video_ts_end: 20, - name: "name", - description: "description", - ux_note: "ux_note", - }, - { - id: 20, - media_id: 20, - video_ts: 10, - video_ts_end: 30, - name: "name", - description: "description", - ux_note: "ux_note", - }, - { - id: 999, - media_id: 999, - video_ts: 10, - video_ts_end: 30, - name: "name", - description: "description", - ux_note: "ux_note", - }, - ]); - await tryber.tables.UxCampaignData.do().insert([ - { - id: 1, - campaign_id: targetCampaignId, - published: 1, - methodology_type: "metodology1", - methodology_description: "methodology_description", - goal: "goal", - users: 15, - }, - { - id: 2, - campaign_id: 9999, - published: 1, - methodology_type: "metodology1", - methodology_description: "methodology_description", - goal: "goal", - users: 15, - }, - ]); - await tryber.tables.UxCampaignSentiments.do().insert([ - { - id: 1, - campaign_id: targetCampaignId, - cluster_id: 10, - value: 5, - comment: "comment", - }, - { - id: 2, - campaign_id: 999, - cluster_id: 99, - value: 1, - comment: "comment2", - }, - ]); - await tryber.tables.MediaTranscripts.do().insert([ - { - id: 11, - media_id: 10, - transcript: transcript_1, - language: "it", - }, - { - id: 22, - media_id: 20, - transcript: transcript_1, - language: "en", - }, - { - id: 999, - media_id: 999, - transcript: transcript_1, - language: "zh", - }, - ]); - }); - afterAll(async () => { - await tryber.tables.WpAppqEvdCampaign.do().delete(); - await tryber.tables.WpAppqCampaignTask.do().delete(); - await tryber.tables.WpAppqCampaignTaskGroup.do().delete(); - await tryber.tables.WpAppqUserTask.do().delete(); - await tryber.tables.WpCrowdAppqHasCandidate.do().delete(); - await tryber.tables.WpAppqUserTaskMedia.do().delete(); - await tryber.tables.WpAppqUsecaseMediaObservations.do().delete(); - await tryber.tables.UxCampaignData.do().delete(); - await tryber.tables.UxCampaignSentiments.do().delete(); - }); - - it("Should insert one row in wp_appq_evd_campaigns", async () => { - const cpDatasBefore = await tryber.tables.WpAppqEvdCampaign.do().select(); - await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - const cpDatasAfter = await tryber.tables.WpAppqEvdCampaign.do().select(); - expect(cpDatasAfter.length).toEqual(cpDatasBefore.length + 1); - }); - it("Return id 0 if the source campaign does not exist", async () => { - const cpId = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: 99999, - }); - expect(cpId).toMatchObject({ - cpIdExperiential: 0, - }); - }); - it("Should insert the experiential campaign with same data the target campaign except projectId", async () => { - const sourceCpData = await tryber.tables.WpAppqEvdCampaign.do() - .select() - .where("id", targetCampaignId) - .first(); - const newCp = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - const newCpData = await tryber.tables.WpAppqEvdCampaign.do() - .select() - .where("id", newCp.cpIdExperiential) - .first(); - expect(newCpData).toEqual({ - ...sourceCpData, - id: newCp.cpIdExperiential, - }); - }); - it("Should return the id of the inserted campaign", async () => { - const newCp = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - expect(newCp).toMatchObject({ - cpIdExperiential: expect.any(Number), - }); - expect(newCp.cpIdExperiential).toBeGreaterThan(0); - }); - it("Should insert the experiential campaign with projectId equal to target project id", async () => { - const newCp = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - const newCpData = await tryber.tables.WpAppqEvdCampaign.do() - .select("id", "project_id") - .where("id", newCp.cpIdExperiential) - .first(); - expect(newCpData).toEqual({ - id: newCp.cpIdExperiential, - project_id: targetPorjectId, - }); - }); - it("Should insert uxData equal to source data with updated campaignId", async () => { - const uxDataBefore = await tryber.tables.UxCampaignData.do().select(); - const newCp = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - const uxDataAfter = await tryber.tables.UxCampaignData.do().select(); - expect(uxDataAfter.length).toBeGreaterThan(uxDataBefore.length); - - const uxColumns = [ - "goal", - "methodology_description", - "methodology_type", - "published", - "users", - ]; - const sourceUxData = await tryber.tables.UxCampaignData.do() - .select(uxColumns) - .where("campaign_id", targetCampaignId) - .first(); - const newUxData = await tryber.tables.UxCampaignData.do() - .select(uxColumns) - .where("campaign_id", newCp.cpIdExperiential) - .first(); - expect(newUxData).toMatchObject(sourceUxData ?? {}); - }); - it("Should insert sentiments", async () => { - const sentimentBefore = - await tryber.tables.UxCampaignSentiments.do().select(); - const newpc = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - const sentimentAfter = - await tryber.tables.UxCampaignSentiments.do().select(); - expect(sentimentAfter.length).toBeGreaterThan(sentimentBefore.length); - - const sourceSentiment = await tryber.tables.UxCampaignSentiments.do() - .select("value", "comment") - .where("campaign_id", targetCampaignId); - expect(sourceSentiment.length).toBe(1); - const newSentiment = await tryber.tables.UxCampaignSentiments.do() - .select("value", "comment") - .where("campaign_id", newpc.cpIdExperiential); - expect(newSentiment.length).toBe(1); - expect(newSentiment[0]).toMatchObject(sourceSentiment[0]); - }); - it("Should insert campaignTasks", async () => { - const campaignTasksBefore = - await tryber.tables.WpAppqCampaignTask.do().select(); - const newpc = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - const campaignTasksAfter = - await tryber.tables.WpAppqCampaignTask.do().select(); - expect(campaignTasksAfter.length).toBeGreaterThan( - campaignTasksBefore.length - ); - const usecasesColumns = [ - "title", - "content", - "is_required", - "simple_title", - "info", - "prefix", - ]; - const sourceCampaignTasks = await tryber.tables.WpAppqCampaignTask.do() - .select(usecasesColumns) - .where("campaign_id", targetCampaignId); - expect(sourceCampaignTasks.length).toBe(1); - const newCampaignTasks = await tryber.tables.WpAppqCampaignTask.do() - .select(usecasesColumns) - .where("campaign_id", newpc.cpIdExperiential); - expect(newCampaignTasks.length).toBe(1); - expect(newCampaignTasks).toMatchObject(sourceCampaignTasks); - }); - it("Should insert userTasks", async () => { - const userTasksBefore = await tryber.tables.WpAppqUserTask.do().select(); - const newpc = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - const userTasksAfter = await tryber.tables.WpAppqUserTask.do().select(); - expect(userTasksAfter.length).toBeGreaterThan(userTasksBefore.length); - const usecasesColumns = ["tester_id", "is_completed"]; - const sourceUserTasks = await tryber.tables.WpAppqUserTask.do() - .select(usecasesColumns) - .whereIn("task_id", [10]); - expect(sourceUserTasks.length).toBe(2); - const newUsecases = ( - await tryber.tables.WpAppqCampaignTask.do() - .select("id") - .where("campaign_id", newpc.cpIdExperiential) - ).map((ct) => ct.id); - const newUserTasks = await tryber.tables.WpAppqUserTask.do() - .select(usecasesColumns) - .whereIn("task_id", newUsecases); - expect(newUserTasks.length).toBe(2); - expect(newUserTasks).toMatchObject(sourceUserTasks); - }); - it("Should insert userTaskMedia", async () => { - const userTaskMediaBefore = - await tryber.tables.WpAppqUserTaskMedia.do().select(); - const newpc = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - //78 - const userTaskMediaAfter = - await tryber.tables.WpAppqUserTaskMedia.do().select(); - expect(userTaskMediaAfter.length).toBeGreaterThan( - userTaskMediaBefore.length - ); - const videoColumns = ["tester_id", "location"]; - const sourceUserTaskMedia = await tryber.tables.WpAppqUserTaskMedia.do() - .select(videoColumns) - .where("campaign_task_id", 10); - expect(sourceUserTaskMedia.length).toBe(1); - const newUsecases = ( - await tryber.tables.WpAppqCampaignTask.do() - .select("id") - .where("campaign_id", newpc.cpIdExperiential) - ).map((ct) => ct.id); - const newUserTaskMedia = await tryber.tables.WpAppqUserTaskMedia.do() - .select(videoColumns) - .whereIn("campaign_task_id", newUsecases); - expect(newUserTaskMedia.length).toBe(1); - expect(newUserTaskMedia[0]).toMatchObject(sourceUserTaskMedia[0]); - }); - - it("Should insert observations", async () => { - const observationsBefore = - await tryber.tables.WpAppqUsecaseMediaObservations.do().select(); - const newpc = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - const observationsAfter = - await tryber.tables.WpAppqUsecaseMediaObservations.do().select(); - expect(observationsAfter.length).toBeGreaterThan(observationsBefore.length); - - const mediaColumns = [ - "video_ts", - "video_ts_end", - "name", - "description", - "ux_note", - ]; - const sourceObservations = - await tryber.tables.WpAppqUsecaseMediaObservations.do() - .select(mediaColumns) - .where("media_id", 10); - expect(sourceObservations.length).toBe(1); - const newCampaignTasksIds = ( - await tryber.tables.WpAppqCampaignTask.do() - .select("id") - .where("campaign_id", newpc.cpIdExperiential) - ).map((ct) => ct.id); - const newMediaIds = ( - await tryber.tables.WpAppqUserTaskMedia.do() - .select("id") - .whereIn("campaign_task_id", newCampaignTasksIds) - ).map((ct) => ct.id); - const newObservations = - await tryber.tables.WpAppqUsecaseMediaObservations.do() - .select(mediaColumns) - .whereIn("media_id", newMediaIds); - expect(newObservations.length).toBe(1); - expect(newObservations).toMatchObject(sourceObservations); - }); - - it("Should insert transcritps", async () => { - const transcriptsBefore = - await tryber.tables.MediaTranscripts.do().select(); - const newpc = await createCpExperiential({ - projectId: targetPorjectId, - sourceCpId: targetCampaignId, - }); - const transcriptsAfter = await tryber.tables.MediaTranscripts.do().select(); - expect(transcriptsAfter.length).toBeGreaterThan(transcriptsBefore.length); - - const newCampaignTasksIds = ( - await tryber.tables.WpAppqCampaignTask.do() - .select("id") - .where("campaign_id", newpc.cpIdExperiential) - ).map((ct) => ct.id); - const newMediaIds = ( - await tryber.tables.WpAppqUserTaskMedia.do() - .select("id") - .whereIn("campaign_task_id", newCampaignTasksIds) - ).map((ct) => ct.id); - const sourceTranscripts = await tryber.tables.MediaTranscripts.do() - .select("transcript", "language") - .whereIn("media_id", [10]); - expect(sourceTranscripts.length).toBe(1); - const newTranscripts = await tryber.tables.MediaTranscripts.do() - .select("transcript", "language") - .whereIn("media_id", newMediaIds); - expect(newTranscripts.length).toBe(1); - expect(newTranscripts).toMatchObject(sourceTranscripts); - }); - // it("Should insert translations", async () => {}); - // it("Should insert tagGroups", async () => {}); - // it("Should insert tags", async () => {}); -}); diff --git a/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.ts b/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.ts deleted file mode 100644 index 450284581..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/createCpExperiential/index.ts +++ /dev/null @@ -1,344 +0,0 @@ -import { copyUxData } from "../copyUxData"; -import { tryber } from "@src/features/database"; -import { getCpData } from "../getCpData"; -import { getExperientialData } from "../getExperientialData"; - -async function createCpExperiential({ - projectId, - sourceCpId, -}: { - projectId: number; - sourceCpId: number; -}): Promise<{ cpIdExperiential: number }> { - const data = await getCpData({ cpId: sourceCpId }); - - if (!data.campaign) return { cpIdExperiential: 0 }; - const taggingToolData = await getExperientialData({ cpId: sourceCpId }); - - const oldCpId = data.campaign.id; - data.campaign.id = undefined; - - const cpIdExperiential = await insertCampaign(); - - data.campaign.id = cpIdExperiential; - data.campaign.oldCpId = oldCpId; - - // IDs mapping OLD_ID -> NEW_ID - const usecaseMap = new Map(); - const userTaskMap = new Map(); - const mediaMap = new Map(); - const tagsTypeMap = new Map(); - const tagsMap = new Map(); - const observationMap = new Map(); - const insightsMap = new Map(); - - if (cpIdExperiential) { - // Basic Datas - await insertCandidates(); - await insertUseCases(); - await insertUserTasks(); - await insertBugs(); - - // Tagging Tool Data - await insertVideosWithTranscriptAndTranslations(); - await insertTagTypes(); - await insertTags(); - await insertObservations(); - await insertTagLinks(); - await copyInsights(); - - // Copy UX Data - ux, questions - await copyUxData({ sourceCpId: sourceCpId, targetCpId: cpIdExperiential }); - // Copy Sentiments - await copySentiments(); - } - - return { cpIdExperiential }; - - async function copySentiments() { - const sentiment = await tryber.tables.UxCampaignSentiments.do() - .select() - .where("campaign_id", sourceCpId); - - if (sentiment.length) { - for (const sent of sentiment) { - if (!usecaseMap.get(sent.cluster_id)) { - console.log( - "error on upload sentiment, not found cluster:id:", - sent.cluster_id - ); - } - await tryber.tables.UxCampaignSentiments.do().insert({ - ...sent, - id: undefined, - campaign_id: cpIdExperiential, - cluster_id: usecaseMap.get(sent.cluster_id), - }); - } - } - } - - async function insertCampaign() { - const newCampaign = await tryber.tables.WpAppqEvdCampaign.do() - .insert({ ...data.campaign, project_id: projectId }) - .returning("id"); - const cpIdExperiential = newCampaign[0].id ?? newCampaign[0]; - return cpIdExperiential ?? 0; - } - - async function insertBugs() { - if (data.bugs) { - for (const bug of data.bugs) { - bug.campaign_id = cpIdExperiential; - const oldBugId = bug.id; - bug.id = undefined; - const usecaseRef = data.usecases?.find( - (usecase) => usecase.oldUsecaseId === bug.application_section_id - ); - bug.application_section = usecaseRef?.title; - bug.application_section_id = usecaseRef?.id; - const newBug = await tryber.tables.WpAppqEvdBug.do() - .insert(bug) - .returning("id"); - const newBugId = newBug[0].id ?? newBug[0]; - bug.id = newBugId; - bug.oldBugId = oldBugId; - - if (data.bugMedias) { - for (const bugMedia of data.bugMedias) { - if (bugMedia.bug_id === bug.oldBugId) { - const oldBugMediaId = bugMedia.id; - bugMedia.bug_id = newBugId; - bugMedia.id = undefined; - const newBugMedia = await tryber.tables.WpAppqEvdBugMedia.do() - .insert(bugMedia) - .returning("id"); - bugMedia.oldBugMediaId = oldBugMediaId; - bugMedia.id = newBugMedia[0].id ?? newBugMedia[0]; - } - } - } - } - } - } - - async function insertUseCases() { - if (!data.usecases) return; - - for (const uc of data.usecases) { - const oldUsecaseId = uc.id; - uc.id = undefined; - uc.campaign_id = cpIdExperiential; - - const newUseCase = await tryber.tables.WpAppqCampaignTask.do() - .insert(uc) - .returning("id"); - const newUseCaseId = newUseCase[0].id ?? newUseCase[0]; - - usecaseMap.set(oldUsecaseId, newUseCaseId); - } - - return true; - } - - async function insertUserTasks() { - if (!data.userTasks) return; - - for (const ut of data.userTasks) { - const oldUserTaskId = ut.id; - ut.id = undefined; - ut.task_id = usecaseMap.get(ut.task_id); - - const newUserTask = await tryber.tables.WpAppqUserTask.do() - .insert(ut) - .returning("id"); - const newUserTaskId = newUserTask[0].id ?? newUserTask[0]; - - userTaskMap.set(oldUserTaskId, newUserTaskId); - } - - return true; - } - - async function insertCandidates() { - if (data.candidates) { - for (const candidate of data.candidates) { - candidate.campaign_id = cpIdExperiential; - await tryber.tables.WpCrowdAppqHasCandidate.do().insert(candidate); - } - } - } - - async function insertVideosWithTranscriptAndTranslations() { - if (!taggingToolData.userTasksMedia) return; - - for (const video of taggingToolData.userTasksMedia) { - const oldVideoId = video.id; - - video.id = undefined; - video.campaign_task_id = usecaseMap.get(video.campaign_task_id); - video.user_task_id = userTaskMap.get(video.user_task_id); - const newVideo = await tryber.tables.WpAppqUserTaskMedia.do() - .insert(video) - .returning("id"); - const newVideoId = newVideo[0].id ?? newVideo[0]; - - mediaMap.set(oldVideoId, newVideoId); - - // Insert Media Transcripts - const trans = await tryber.tables.MediaTranscripts.do() - .select() - .where("media_id", oldVideoId) - .first(); - - if (trans) { - await tryber.tables.MediaTranscripts.do().insert({ - ...trans, - id: undefined, - media_id: newVideoId, - }); - } - - /** - * Check if there are any translations for this video - */ - const translation = await tryber.tables.MediaTranscriptsTranslations.do() - .select() - .where("media_id", oldVideoId) - .first(); - - if (translation) { - await tryber.tables.MediaTranscriptsTranslations.do().insert({ - ...translation, - id: undefined, - media_id: newVideoId, - }); - } - } - - return true; - } - - async function insertTagTypes() { - if (!taggingToolData.tagTypes) return; - - for (const tagType of taggingToolData.tagTypes) { - const oldTagTypeId = tagType.id; - - tagType.id = undefined; - tagType.campaign_id = cpIdExperiential; - - const newTagType = await tryber.tables.WpAppqUsecaseMediaTagType.do() - .insert(tagType) - .returning("id"); - - const newTagTypeId = newTagType[0].id ?? newTagType[0]; - tagsTypeMap.set(oldTagTypeId, newTagTypeId); - } - - return true; - } - - async function insertTags() { - if (!taggingToolData.tags) return; - - for (const tag of taggingToolData.tags) { - const oldTagId = tag.id; - - tag.id = undefined; - tag.type = tagsTypeMap.get(tag.type); - - const newTag = await tryber.tables.WpAppqUsecaseMediaObservationsTags.do() - .insert(tag) - .returning("id"); - - const newTagId = newTag[0].id ?? newTag[0]; - tagsMap.set(oldTagId, newTagId); - } - - return true; - } - - async function insertObservations() { - if (!taggingToolData.observations) return; - - for (const observation of taggingToolData.observations) { - const oldObservationId = observation.id; - - observation.id = undefined; - observation.media_id = mediaMap.get(observation.media_id); - - const newObservation = - await tryber.tables.WpAppqUsecaseMediaObservations.do() - .insert(observation) - .returning("id"); - - observation.id = newObservation[0].id ?? newObservation[0]; - observationMap.set(oldObservationId, observation.id); - } - - return true; - } - - async function insertTagLinks() { - if (!taggingToolData.tagLinks) return; - - for (const tagLink of taggingToolData.tagLinks) { - tagLink.id = undefined; - tagLink.tag_id = tagsMap.get(tagLink.tag_id); - tagLink.observation_id = observationMap.get(tagLink.observation_id); - - await tryber.tables.WpAppqUsecaseMediaObservationsTagsLink.do().insert( - tagLink - ); - } - - return true; - } - - async function copyInsights() { - const insights = await tryber.tables.UxCampaignInsights.do() - .select() - .where("campaign_id", sourceCpId); - - if (!insights.length) return; - - for (const insight of insights) { - const oldInsightId = insight.id; - - const newInsight = await tryber.tables.UxCampaignInsights.do() - .insert({ - ...insight, - id: undefined, - campaign_id: cpIdExperiential, - severity_id: tagsMap.get(insight.severity_id), - }) - .returning("id"); - - const newInsightId = newInsight[0].id ?? newInsight[0]; - insightsMap.set(oldInsightId, newInsightId); - } - - // Link insights to observations - const insightObservations = - await tryber.tables.UxCampaignInsightsToObservations.do() - .select() - .whereIn( - "insight_id", - insights.map((insight) => insight.id) - ); - - if (insightObservations.length) { - for (const insightObservation of insightObservations) { - await tryber.tables.UxCampaignInsightsToObservations.do().insert({ - ...insightObservation, - id: undefined, - insight_id: insightsMap.get(insightObservation.insight_id), - observation_id: observationMap.get(insightObservation.observation_id), - }); - } - } - } -} - -export { createCpExperiential }; diff --git a/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.spec.ts deleted file mode 100644 index 511a26f30..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.spec.ts +++ /dev/null @@ -1,333 +0,0 @@ -import { createCpFunctional } from "."; -import { tryber } from "@src/features/database"; - -describe("createCpFunctional", () => { - const targetCampaignId = 69; - const targetProjectId = 6969; - - beforeAll(async () => { - await tryber.tables.WpAppqEvdCampaign.do().insert({ - id: targetCampaignId, - title: "Title - Functional Campaign", - description: "Functional Campaign", - campaign_type: 1, - campaign_type_id: 1, - project_id: 6969, - platform_id: 1, - start_date: "2021-01-01", - end_date: "2021-01-01", - page_preview_id: 1, - page_manual_id: 1, - pm_id: 1, - customer_id: 1, - customer_title: "Customer Title - Functional Campaign", - }); - await tryber.tables.WpAppqCampaignTask.do().insert([ - { - id: 10, - campaign_id: targetCampaignId, - title: "Title - Campaign Task", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - { - id: 20, - campaign_id: 69, - title: "Title - Campaign Task", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - { - id: 999, - campaign_id: 9999, - title: "Title - Campaign Task other cp", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - ]); - await tryber.tables.WpAppqCampaignTaskGroup.do().insert([ - { - task_id: 10, - group_id: 1, - }, - { - task_id: 20, - group_id: 0, - }, - { - task_id: 999, - group_id: 0, - }, - ]); - await tryber.tables.WpAppqUserTask.do().insert([ - { - id: 100, - task_id: 10, - tester_id: 11111, - is_completed: 1, - }, - { - id: 101, - task_id: 10, - tester_id: 32, - is_completed: 0, - }, - { - id: 102, - task_id: 999, - tester_id: 32, - is_completed: 1, - }, - ]); - await tryber.tables.WpCrowdAppqHasCandidate.do().insert([ - { - user_id: 11111, - campaign_id: targetCampaignId, - }, - { - user_id: 32, - campaign_id: targetCampaignId, - }, - { - user_id: 32, - campaign_id: 9999, - }, - ]); - await tryber.tables.WpAppqEvdBug.do().insert([ - { - id: 1, - campaign_id: targetCampaignId, - wp_user_id: 11111, - reviewer: 32, - last_editor_id: 11111, - }, - { - id: 2, - campaign_id: targetCampaignId, - wp_user_id: 32, - reviewer: 11111, - last_editor_id: 11111, - }, - { - id: 999, - campaign_id: 9999, - wp_user_id: 11111, - reviewer: 32, - last_editor_id: 11111, - }, - ]); - await tryber.tables.WpAppqEvdBugMedia.do().insert([ - { - id: 10, - bug_id: 1, - }, - { - id: 20, - bug_id: 2, - }, - { - id: 999, - bug_id: 999, - }, - ]); - }); - afterAll(async () => { - await tryber.tables.WpAppqEvdCampaign.do().delete(); - await tryber.tables.WpAppqCampaignTask.do().delete(); - await tryber.tables.WpAppqCampaignTaskGroup.do().delete(); - await tryber.tables.WpAppqUserTask.do().delete(); - await tryber.tables.WpCrowdAppqHasCandidate.do().delete(); - await tryber.tables.WpAppqEvdBug.do().delete(); - await tryber.tables.WpAppqEvdBugMedia.do().delete(); - }); - - it("Should insert one row in wp_appq_evd_campaigns", async () => { - const campaignBefore = await tryber.tables.WpAppqEvdCampaign.do().select(); - - await createCpFunctional({ - projectId: targetProjectId, - sourceCpId: targetCampaignId, - }); - const campaignAfter = await tryber.tables.WpAppqEvdCampaign.do().select(); - expect(campaignAfter.length).toBe(campaignBefore.length + 1); - }); - it("Should insert the functional campaign with same data the target campaign except projectId", async () => { - const campaign = await tryber.tables.WpAppqEvdCampaign.do() - .select() - .where("id", targetCampaignId) - .first(); - - const { cpIdFunctional: newCampaignId } = await createCpFunctional({ - projectId: targetCampaignId, - sourceCpId: targetCampaignId, - }); - - const newCampaign = await tryber.tables.WpAppqEvdCampaign.do() - .select() - .where("id", newCampaignId) - .first(); - - expect(newCampaign).toMatchObject({ - title: campaign?.title, - description: campaign?.description, - campaign_type: campaign?.campaign_type, - campaign_type_id: campaign?.campaign_type_id, - platform_id: campaign?.platform_id, - start_date: campaign?.start_date, - end_date: campaign?.end_date, - page_preview_id: campaign?.page_preview_id, - page_manual_id: campaign?.page_manual_id, - pm_id: campaign?.pm_id, - customer_id: campaign?.customer_id, - customer_title: campaign?.customer_title, - }); - }); - it("Should insert the functional campaign with target projectId", async () => { - const { cpIdFunctional: newCampaignId } = await createCpFunctional({ - projectId: targetProjectId, - sourceCpId: targetCampaignId, - }); - - const newCampaign = await tryber.tables.WpAppqEvdCampaign.do() - .select() - .where("id", newCampaignId) - .first(); - - expect(newCampaign).toMatchObject({ - project_id: targetProjectId, - }); - }); - it("Should insert the same number of candidates of target-campaign candidates", async () => { - const targetCandidates = await tryber.tables.WpCrowdAppqHasCandidate.do() - .select() - .where("campaign_id", targetCampaignId); - - const { cpIdFunctional: newCampaignId } = await createCpFunctional({ - projectId: targetProjectId, - sourceCpId: targetCampaignId, - }); - - const newCandidates = await tryber.tables.WpCrowdAppqHasCandidate.do() - .select() - .where("campaign_id", newCampaignId); - expect(newCandidates.length).toEqual(targetCandidates.length); - }); - it("Should insert the same number of usecases of target-usecases", async () => { - const targetTasks = await tryber.tables.WpAppqCampaignTask.do() - .select() - .where("campaign_id", targetCampaignId); - - const { cpIdFunctional: newCampaignId } = await createCpFunctional({ - projectId: targetProjectId, - sourceCpId: targetCampaignId, - }); - - const newTasks = await tryber.tables.WpAppqCampaignTask.do() - .select() - .where("campaign_id", newCampaignId); - expect(newTasks.length).toEqual(targetTasks.length); - }); - // it("Should insert the same number of task-groups of target-campaign task-groups", async () => { - // const targetTaskGroups = await tryber.tables.WpAppqCampaignTaskGroup.do() - // .select() - // .where("campaign_id", targetCampaignId); - - // const { cpIdFunctional: newCampaignId } = await createCpFunctional({ - // projectId: targetProjectId, - // sourceCpId: targetCampaignId, - // }); - - // const newTaskGroups = await tryber.tables.WpAppqCampaignTaskGroup.do() - // .select() - // .where("campaign_id", newCampaignId); - // expect(newTaskGroups.length).toEqual(targetTaskGroups.length); - // }); - it("Should insert the same number of user-tasks of target-campaign user-tasks", async () => { - const targetTaskIds = await tryber.tables.WpAppqCampaignTask.do() - .select("id") - .where("campaign_id", targetCampaignId); - - const targetUserTasks = await tryber.tables.WpAppqUserTask.do() - .select() - .whereIn( - "task_id", - targetTaskIds.map((task) => task.id) - ); - - const { cpIdFunctional: newCampaignId } = await createCpFunctional({ - projectId: targetProjectId, - sourceCpId: targetCampaignId, - }); - - const newTaskIds = await tryber.tables.WpAppqCampaignTask.do() - .select("id") - .where("campaign_id", newCampaignId); - - const newUserTasks = await tryber.tables.WpAppqUserTask.do() - .select() - .whereIn( - "task_id", - newTaskIds.map((task) => task.id) - ); - expect(newUserTasks.length).toEqual(targetUserTasks.length); - }); - it("Should insert the same number of bugs of target-campaign bugs", async () => { - const targetBugs = await tryber.tables.WpAppqEvdBug.do() - .select() - .where("campaign_id", targetCampaignId); - - const { cpIdFunctional: newCampaignId } = await createCpFunctional({ - projectId: targetProjectId, - sourceCpId: targetCampaignId, - }); - - const newBugs = await tryber.tables.WpAppqEvdBug.do() - .select() - .where("campaign_id", newCampaignId); - expect(newBugs.length).toEqual(targetBugs.length); - }); - it("Should insert the same number of bug-medias of target-campaign bug-medias", async () => { - const targetBugIds = await tryber.tables.WpAppqEvdBug.do() - .select("id") - .where("campaign_id", targetCampaignId); - - const targetBugMedias = await tryber.tables.WpAppqEvdBugMedia.do() - .select() - .whereIn( - "bug_id", - targetBugIds.map((bug) => bug.id) - ); - - const { cpIdFunctional: newCampaignId } = await createCpFunctional({ - projectId: targetProjectId, - sourceCpId: targetCampaignId, - }); - - const newBugIds = await tryber.tables.WpAppqEvdBug.do() - .select("id") - .where("campaign_id", newCampaignId); - - const newBugMedias = await tryber.tables.WpAppqEvdBugMedia.do() - .select() - .whereIn( - "bug_id", - newBugIds.map((bug) => bug.id) - ); - expect(newBugMedias.length).toEqual(targetBugMedias.length); - }); -}); diff --git a/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.ts b/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.ts deleted file mode 100644 index 9c1a9d9b1..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/createCpFunctional/index.ts +++ /dev/null @@ -1,119 +0,0 @@ -import { tryber } from "@src/features/database"; -import { getCpData } from "../getCpData"; - -async function createCpFunctional({ - projectId, - sourceCpId, -}: { - projectId: number; - sourceCpId: number; -}): Promise<{ cpIdFunctional: number }> { - const data = await getCpData({ cpId: sourceCpId }); - - if (!data.campaign) return { cpIdFunctional: 0 }; - - const oldCpId = data.campaign.id; - data.campaign.id = undefined; - - const cpIdFunctional = await insertCampaign(); - - data.campaign.id = cpIdFunctional; - data.campaign.oldCpId = oldCpId; - - if (cpIdFunctional) { - await insertCandidates(); - - if (data.usecases) { - for (const usecase of data.usecases) { - const { oldUsecaseId, newUsecaseId } = await insertUsecase(usecase); - - await insertUserTasks(oldUsecaseId, newUsecaseId); - } - } - - await insertBugs(); - } - - return { cpIdFunctional }; - - async function insertCampaign() { - const newCampaign = await tryber.tables.WpAppqEvdCampaign.do() - .insert({ ...data.campaign, project_id: projectId }) - .returning("id"); - const cpIdFunctional = newCampaign[0].id ?? newCampaign[0]; - return cpIdFunctional ?? 0; - } - - async function insertBugs() { - if (data.bugs) { - for (const bug of data.bugs) { - bug.campaign_id = cpIdFunctional; - const oldBugId = bug.id; - bug.id = undefined; - const usecaseRef = data.usecases?.find( - (usecase) => usecase.oldUsecaseId === bug.application_section_id - ); - bug.application_section = usecaseRef?.title; - bug.application_section_id = usecaseRef?.id; - const newBug = await tryber.tables.WpAppqEvdBug.do() - .insert(bug) - .returning("id"); - const newBugId = newBug[0].id ?? newBug[0]; - bug.id = newBugId; - bug.oldBugId = oldBugId; - - if (data.bugMedias) { - for (const bugMedia of data.bugMedias) { - if (bugMedia.bug_id === bug.oldBugId) { - const oldBugMediaId = bugMedia.id; - bugMedia.bug_id = newBugId; - bugMedia.id = undefined; - const newBugMedia = await tryber.tables.WpAppqEvdBugMedia.do() - .insert(bugMedia) - .returning("id"); - bugMedia.oldBugMediaId = oldBugMediaId; - bugMedia.id = newBugMedia[0].id ?? newBugMedia[0]; - } - } - } - } - } - } - - async function insertUsecase(usecase: any) { - usecase.campaign_id = cpIdFunctional; - const oldUsecaseId = usecase.id; - usecase.id = undefined; - const newUsecase = await tryber.tables.WpAppqCampaignTask.do() - .insert(usecase) - .returning("id"); - const newUsecaseId = newUsecase[0].id ?? newUsecase[0]; - usecase.id = newUsecaseId; - usecase.campaign_id = cpIdFunctional; - usecase.oldUsecaseId = oldUsecaseId; - return { oldUsecaseId, newUsecaseId }; - } - - async function insertUserTasks(oldUsecaseId: any, newUsecaseId: number) { - if (data.userTasks) { - for (const userTask of data.userTasks) { - if (userTask.task_id === oldUsecaseId) { - userTask.task_id = newUsecaseId; - userTask.id = undefined; - await tryber.tables.WpAppqUserTask.do().insert(userTask); - } - } - } - } - - async function insertCandidates() { - if (data.candidates) { - for (const candidate of data.candidates) { - candidate.campaign_id = cpIdFunctional; - await tryber.tables.WpCrowdAppqHasCandidate.do().insert(candidate); - } - } - } -} - -export { createCpFunctional }; diff --git a/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.spec.ts deleted file mode 100644 index 71745e4e9..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.spec.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { createDemoProject } from "."; -import { tryber } from "@src/features/database"; - -describe("createDemoProject", () => { - afterEach(async () => { - await tryber.tables.WpAppqProject.do().delete(); - }); - - it("Should insert the demo project", async () => { - const workspaceId = 69; - - const projectsBefore = await tryber.tables.WpAppqProject.do() - .select() - .where({ display_name: "Demo Project" }); - - expect(projectsBefore.length).toBe(0); - const { projectId } = await createDemoProject({ workspaceId }); - - const progetsAfter = await tryber.tables.WpAppqProject.do().select(); - - expect(progetsAfter.length).toBe(1); - expect(progetsAfter[0]).toMatchObject({ - id: projectId, - display_name: "Demo Project", - customer_id: workspaceId, - edited_by: 0, - }); - }); - it("Should return the id", async () => { - const workspaceId = 69; - const projectsBefore = await tryber.tables.WpAppqProject.do().select(); - - expect(projectsBefore.length).toBe(0); - const { projectId } = await createDemoProject({ workspaceId }); - - const progetsAfter = await tryber.tables.WpAppqProject.do().select(); - - expect(progetsAfter[0].id).toBe(projectId); - }); - - it("Should insert the project releated to workspace", async () => { - const workspaceId = 69; - const projectsbefore = await tryber.tables.WpAppqProject.do().select(); - expect(projectsbefore.length).toBe(0); - - const { projectId } = await createDemoProject({ workspaceId }); - const projects = await tryber.tables.WpAppqProject.do() - .select() - .where("id", projectId); - - expect(projects.length).toBe(1); - }); -}); diff --git a/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.ts b/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.ts deleted file mode 100644 index 93d71fbed..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/createDemoProject/index.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { tryber } from "@src/features/database"; - -async function createDemoProject({ - workspaceId, -}: { - workspaceId: number; -}): Promise<{ projectId: number }> { - const project = await tryber.tables.WpAppqProject.do() - .insert({ - display_name: "Demo Project", - customer_id: workspaceId, - edited_by: 0, - }) - .returning("id"); - const projectId = project[0].id ?? project[0]; - - return { projectId }; -} - -export { createDemoProject }; diff --git a/src/routes/customers/_post/createDemoEnvironment/getCpData/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/getCpData/index.spec.ts deleted file mode 100644 index b6ccc3d47..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/getCpData/index.spec.ts +++ /dev/null @@ -1,315 +0,0 @@ -import { getCpData } from "."; -import { tryber } from "@src/features/database"; - -describe("getCpData", () => { - beforeAll(async () => { - await tryber.tables.WpAppqEvdCampaign.do().insert({ - id: 69, - title: "Title - Functional Campaign", - description: "Functional Campaign", - campaign_type: 1, - campaign_type_id: 1, - project_id: 6969, - platform_id: 1, - start_date: "2021-01-01", - end_date: "2021-01-01", - page_preview_id: 1, - page_manual_id: 1, - pm_id: 1, - customer_id: 1, - customer_title: "Customer Title - Functional Campaign", - }); - await tryber.tables.WpAppqCampaignTask.do().insert([ - { - id: 10, - campaign_id: 69, - title: "Title - Campaign Task", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - { - id: 20, - campaign_id: 69, - title: "Title - Campaign Task", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - { - id: 999, - campaign_id: 9999, - title: "Title - Campaign Task other cp", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - ]); - await tryber.tables.WpAppqCampaignTaskGroup.do().insert([ - { - task_id: 10, - group_id: 1, - }, - { - task_id: 20, - group_id: 0, - }, - { - task_id: 999, - group_id: 0, - }, - ]); - await tryber.tables.WpAppqUserTask.do().insert([ - { - id: 100, - task_id: 10, - tester_id: 11111, - is_completed: 1, - }, - { - id: 101, - task_id: 10, - tester_id: 32, - is_completed: 0, - }, - { - id: 102, - task_id: 999, - tester_id: 32, - is_completed: 1, - }, - ]); - await tryber.tables.WpCrowdAppqHasCandidate.do().insert([ - { - user_id: 11111, - campaign_id: 69, - }, - { - user_id: 32, - campaign_id: 69, - }, - { - user_id: 32, - campaign_id: 9999, - }, - ]); - await tryber.tables.WpAppqEvdBug.do().insert([ - { - id: 1, - campaign_id: 69, - wp_user_id: 11111, - reviewer: 32, - last_editor_id: 11111, - }, - { - id: 2, - campaign_id: 69, - wp_user_id: 32, - reviewer: 11111, - last_editor_id: 11111, - }, - { - id: 999, - campaign_id: 9999, - wp_user_id: 11111, - reviewer: 32, - last_editor_id: 11111, - }, - ]); - await tryber.tables.WpAppqEvdBugMedia.do().insert([ - { - id: 10, - bug_id: 1, - }, - { - id: 20, - bug_id: 2, - }, - { - id: 999, - bug_id: 999, - }, - ]); - }); - afterAll(async () => { - await tryber.tables.WpAppqEvdCampaign.do().delete(); - await tryber.tables.WpAppqCampaignTask.do().delete(); - await tryber.tables.WpAppqCampaignTaskGroup.do().delete(); - await tryber.tables.WpAppqUserTask.do().delete(); - await tryber.tables.WpCrowdAppqHasCandidate.do().delete(); - await tryber.tables.WpAppqEvdBug.do().delete(); - await tryber.tables.WpAppqEvdBugMedia.do().delete(); - }); - - it("Should return empty object if campaign does not exist", async () => { - const cpId = 9999; - const campaigns = await tryber.tables.WpAppqEvdCampaign.do() - .select() - .where("id", cpId); - expect(campaigns.length).toBe(0); - const cpData = await getCpData({ cpId }); - - expect(cpData).toBeInstanceOf(Object); - expect(cpData).toMatchObject({}); - }); - - it("Should return campaign data", async () => { - const cpId = 69; - const campaigns = await tryber.tables.WpAppqEvdCampaign.do() - .select() - .where("id", cpId); - expect(campaigns.length).toBe(1); - const data = await getCpData({ cpId }); - - expect(data).toHaveProperty("campaign"); - expect(data.campaign).toMatchObject({ - id: 69, - title: "Title - Functional Campaign", - description: "Functional Campaign", - campaign_type: 1, - campaign_type_id: 1, - project_id: 6969, - platform_id: 1, - start_date: "2021-01-01", - end_date: "2021-01-01", - page_preview_id: 1, - page_manual_id: 1, - pm_id: 1, - customer_id: 1, - customer_title: "Customer Title - Functional Campaign", - }); - }); - - it("Should return campaign usecases if campagin has usecases", async () => { - const cpId = 69; - const data = await getCpData({ cpId }); - - expect(data).toHaveProperty("usecases"); - expect(data.usecases).toHaveLength(2); - expect(data.usecases).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 10, - campaign_id: 69, - title: "Title - Campaign Task", - }), - expect.objectContaining({ - id: 20, - campaign_id: 69, - title: "Title - Campaign Task", - }), - ]) - ); - }); - it("Should return campaign usecaseGroups if campagin has group of usecases", async () => { - const cpId = 69; - const data = await getCpData({ cpId }); - - expect(data).toHaveProperty("usecaseGroups"); - expect(data.usecaseGroups).toHaveLength(2); - expect(data.usecaseGroups).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - task_id: 10, - group_id: 1, - }), - expect.objectContaining({ - task_id: 20, - group_id: 0, - }), - ]) - ); - }); - it("Should return campaign userTask if campagin has user tasks", async () => { - const cpId = 69; - const data = await getCpData({ cpId }); - expect(data).toHaveProperty("userTasks"); - expect(data.userTasks).toHaveLength(2); - expect(data.userTasks).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 100, - task_id: 10, - tester_id: 11111, - }), - expect.objectContaining({ - id: 101, - task_id: 10, - tester_id: 32, - }), - ]) - ); - }); - it("Should return campaign candidates if campagin has candidates", async () => { - const cpId = 69; - const data = await getCpData({ cpId }); - expect(data).toHaveProperty("candidates"); - expect(data.candidates).toHaveLength(2); - expect(data.candidates).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - user_id: 11111, - campaign_id: 69, - }), - expect.objectContaining({ - user_id: 32, - campaign_id: 69, - }), - ]) - ); - }); - it("Should return campaign bugs if campaign has bugs", async () => { - const cpId = 69; - const data = await getCpData({ cpId }); - expect(data).toHaveProperty("bugs"); - expect(data.bugs).toHaveLength(2); - expect(data.bugs).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 1, - campaign_id: 69, - wp_user_id: 11111, - reviewer: 32, - last_editor_id: 11111, - }), - expect.objectContaining({ - id: 2, - campaign_id: 69, - wp_user_id: 32, - reviewer: 11111, - last_editor_id: 11111, - }), - ]) - ); - }); - it("Should return bugMedias if campaign has bugMedias", async () => { - const cpId = 69; - const data = await getCpData({ cpId }); - expect(data).toHaveProperty("bugMedias"); - expect(data.bugMedias).toHaveLength(2); - expect(data.bugMedias).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 10, - bug_id: 1, - }), - expect.objectContaining({ - id: 20, - bug_id: 2, - }), - ]) - ); - }); -}); diff --git a/src/routes/customers/_post/createDemoEnvironment/getCpData/index.ts b/src/routes/customers/_post/createDemoEnvironment/getCpData/index.ts deleted file mode 100644 index b06ab9361..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/getCpData/index.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { tryber } from "@src/features/database"; - -interface CampaignData { - campaign?: any; - usecases?: any[]; - usecaseGroups?: any[]; - userTasks?: any[]; - candidates?: any[]; - bugs?: any[]; - bugMedias?: any[]; -} - -async function getCpData({ cpId }: { cpId: number }): Promise { - const res: CampaignData = {}; - const cpData = await tryber.tables.WpAppqEvdCampaign.do() - .select() - .where("id", cpId) - .first(); - if (cpData) { - res.campaign = cpData; - } - - const usecases = await tryber.tables.WpAppqCampaignTask.do() - .select() - .where("campaign_id", cpId); - if (usecases.length > 0) { - res.usecases = usecases; - - const usecaseGroups = await tryber.tables.WpAppqCampaignTaskGroup.do() - .select() - .whereIn( - "task_id", - usecases.map((usecase) => usecase.id) - ); - if (usecaseGroups.length > 0) { - res.usecaseGroups = usecaseGroups; - } - - const userTasks = await tryber.tables.WpAppqUserTask.do() - .select() - .whereIn( - "task_id", - usecases.map((usecase) => usecase.id) - ); - if (userTasks.length > 0) { - res.userTasks = userTasks; - } - } - - const candidates = await tryber.tables.WpCrowdAppqHasCandidate.do() - .select() - .where("campaign_id", cpId); - if (candidates.length > 0) { - res.candidates = candidates; - } - - const bugs = await tryber.tables.WpAppqEvdBug.do() - .select() - .where("campaign_id", cpId); - - if (bugs.length > 0) { - res.bugs = bugs; - - const bugMedias = await tryber.tables.WpAppqEvdBugMedia.do() - .select() - .whereIn( - "bug_id", - bugs.map((bug) => bug.id) - ); - if (bugMedias.length > 0) { - res.bugMedias = bugMedias; - } - } - - return res; -} - -export { getCpData }; diff --git a/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.spec.ts deleted file mode 100644 index 684d4b2d6..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.spec.ts +++ /dev/null @@ -1,408 +0,0 @@ -import { getExperientialData } from "."; -import { tryber } from "@src/features/database"; -const targetCpId = 69; -const transcript_1 = `{ - "speakers": 2, - "paragraphs": [ - { - "end": 0.65999997, - "text": "Bene?", - "start": 0.16, - "words": [ - { - "end": 0.65999997, - "word": "Bene?", - "start": 0.16, - "speaker": 0 - } - ], - "speaker": 0 - }, { - "end": 2.6599998, - "text": "Sì la vedo la sento bene", - "start": 0.88, - "words": [ - { - "end": 1.1999999, - "word": "Sì", - "start": 0.88, - "speaker": 1 - }, { - "end": 1.4399999, - "word": "la", - "start": 1.1999999, - "speaker": 1 - }, { - "end": 1.5999999, - "word": "vedo", - "start": 1.4399999, - "speaker": 1 - }, { - "end": 1.8399999, - "word": "la", - "start": 1.5999999, - "speaker": 1 - } - ] - } - ] -}`; -describe("getExperientialData", () => { - beforeAll(async () => { - await tryber.tables.WpAppqEvdCampaign.do().insert({ - id: 69, - title: "Title - Functional Campaign", - description: "Functional Campaign", - campaign_type: 1, - campaign_type_id: 1, - project_id: 6969, - platform_id: 1, - start_date: "2021-01-01", - end_date: "2021-01-01", - page_preview_id: 1, - page_manual_id: 1, - pm_id: 1, - customer_id: 1, - customer_title: "Customer Title - Functional Campaign", - }); - await tryber.tables.WpAppqCampaignTask.do().insert([ - { - id: 10, - campaign_id: 69, - title: "Title - Campaign Task", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - { - id: 20, - campaign_id: 69, - title: "Title - Campaign Task", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - { - id: 999, - campaign_id: 9999, - title: "Title - Campaign Task other cp", - content: "Functional Campaign", - jf_code: "jf_code", - jf_text: "jf_text", - is_required: 1, - simple_title: "simple_title", - info: "info", - prefix: "prefix", - }, - ]); - await tryber.tables.WpAppqUserTaskMedia.do().insert([ - { - id: 10, - campaign_task_id: 10, - user_task_id: 0, - tester_id: 11111, - location: "location", - }, - { - id: 20, - campaign_task_id: 20, - user_task_id: 0, - tester_id: 11111, - location: "location", - }, - { - id: 999, - campaign_task_id: 999, - user_task_id: 0, - tester_id: 32, - location: "location", - }, - ]); - await tryber.tables.WpAppqUsecaseMediaObservations.do().insert([ - { - id: 10, - media_id: 10, - video_ts: 10, - video_ts_end: 20, - name: "name", - description: "description", - ux_note: "ux_note", - }, - { - id: 20, - media_id: 20, - video_ts: 10, - video_ts_end: 30, - name: "name", - description: "description", - ux_note: "ux_note", - }, - { - id: 999, - media_id: 999, - video_ts: 10, - video_ts_end: 30, - name: "name", - description: "description", - ux_note: "ux_note", - }, - ]); - await tryber.tables.WpAppqUsecaseMediaTagType.do().insert([ - { - id: 10, - campaign_id: targetCpId, - name: "tagGroup10", - }, - { - id: 20, - campaign_id: targetCpId, - name: "tagGroup20", - }, - { - id: 999, - campaign_id: 99999, - name: "tagGroup999", - }, - ]); - await tryber.tables.WpAppqUsecaseMediaObservationsTags.do().insert([ - { id: 10, type: 10, name: "tagName10", style: "white" }, - { id: 20, type: 10, name: "tagName20", style: "red" }, - { id: 30, type: 20, name: "tagName30", style: "green" }, - { id: 999, type: 999, name: "tagName999", style: "blue" }, - ]); - await tryber.tables.WpAppqUsecaseMediaObservationsTagsLink.do().insert([ - { id: 10, tag_id: 10, observation_id: 10 }, - { id: 20, tag_id: 20, observation_id: 20 }, - { id: 30, tag_id: 30, observation_id: 20 }, - { id: 999, tag_id: 999, observation_id: 999 }, - ]); - await tryber.tables.MediaTranscripts.do().insert([ - { - id: 11, - media_id: 10, - transcript: transcript_1, - language: "it", - }, - { - id: 22, - media_id: 20, - transcript: transcript_1, - language: "en", - }, - { - id: 999, - media_id: 999, - transcript: transcript_1, - language: "zh", - }, - ]); - await tryber.tables.MediaTranscriptsTranslations.do().insert([ - { id: 11, media_id: 10, translation: "translation", language: "en" }, - { id: 22, media_id: 20, translation: "translation", language: "it" }, - { id: 999, media_id: 999, translation: "translation", language: "it" }, - ]); - }); - afterAll(async () => { - await tryber.tables.WpAppqEvdCampaign.do().delete(); - await tryber.tables.WpAppqCampaignTask.do().delete(); - await tryber.tables.WpAppqUserTaskMedia.do().delete(); - await tryber.tables.WpAppqUsecaseMediaObservations.do().delete(); - await tryber.tables.WpAppqUsecaseMediaTagType.do().delete(); - await tryber.tables.WpAppqUsecaseMediaObservationsTags.do().delete(); - await tryber.tables.WpAppqUsecaseMediaObservationsTagsLink.do().delete(); - await tryber.tables.MediaTranscripts.do().delete(); - await tryber.tables.MediaTranscriptsTranslations.do().delete(); - }); - - it("Should return empty object if campaign does not exist", async () => { - const cpId = 9999; - const campaigns = await tryber.tables.WpAppqEvdCampaign.do() - .select() - .where("id", cpId); - expect(campaigns.length).toBe(0); - const cpData = await getExperientialData({ cpId }); - - expect(cpData).toBeInstanceOf(Object); - expect(cpData).toMatchObject({}); - }); - - it("Should return userTasksMedia if campagin has usreTasksMedia", async () => { - const data = await getExperientialData({ cpId: targetCpId }); - - expect(data).toHaveProperty("userTasksMedia"); - expect(data.userTasksMedia).toHaveLength(2); - expect(data.userTasksMedia).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 10, - tester_id: 11111, - campaign_task_id: 10, - user_task_id: 0, - }), - expect.objectContaining({ - id: 20, - tester_id: 11111, - campaign_task_id: 20, - user_task_id: 0, - }), - ]) - ); - }); - it("Should return campaign observation if campagin has observation", async () => { - const data = await getExperientialData({ cpId: targetCpId }); - - expect(data).toHaveProperty("observations"); - expect(data.observations).toHaveLength(2); - expect(data.observations).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 10, - media_id: 10, - video_ts: 10, - video_ts_end: 20, - name: "name", - description: "description", - ux_note: "ux_note", - }), - expect.objectContaining({ - id: 20, - media_id: 20, - video_ts: 10, - video_ts_end: 30, - name: "name", - description: "description", - ux_note: "ux_note", - }), - ]) - ); - }); - it("Should return all campaign tags", async () => { - const data = await getExperientialData({ cpId: targetCpId }); - - expect(data).toHaveProperty("tags"); - expect(data.tags).toHaveLength(3); - expect(data.tags).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 10, - type: 10, - name: "tagName10", - style: "white", - created_at: expect.any(String), - updated_at: expect.any(String), - }), - expect.objectContaining({ - id: 20, - type: 10, - name: "tagName20", - style: "red", - created_at: expect.any(String), - updated_at: expect.any(String), - }), - expect.objectContaining({ - id: 30, - type: 20, - name: "tagName30", - style: "green", - created_at: expect.any(String), - updated_at: expect.any(String), - }), - ]) - ); - }); - it("Should return all campaign task groups", async () => { - const data = await getExperientialData({ cpId: targetCpId }); - - expect(data).toHaveProperty("tagTypes"); - expect(data.tagTypes).toHaveLength(2); - expect(data.tagTypes).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 10, - campaign_id: targetCpId, - name: "tagGroup10", - }), - expect.objectContaining({ - id: 20, - campaign_id: targetCpId, - name: "tagGroup20", - }), - ]) - ); - }); - it("Should return all tagsLinks with observations", async () => { - const data = await getExperientialData({ cpId: targetCpId }); - - expect(data).toHaveProperty("tagLinks"); - expect(data.tagLinks).toHaveLength(3); - expect(data.tagLinks).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 10, - tag_id: 10, - observation_id: 10, - }), - expect.objectContaining({ - id: 20, - tag_id: 20, - observation_id: 20, - }), - expect.objectContaining({ - id: 30, - tag_id: 30, - observation_id: 20, - }), - ]) - ); - }); - it("Should return all campaign transcripts", async () => { - const data = await getExperientialData({ cpId: targetCpId }); - - expect(data).toHaveProperty("transcripts"); - expect(data.transcripts).toHaveLength(2); - expect(data.transcripts).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 11, - media_id: 10, - transcript: transcript_1, - }), - expect.objectContaining({ - id: 22, - media_id: 20, - transcript: transcript_1, - }), - ]) - ); - }); - it("Should return all campaign transcript translations", async () => { - const data = await getExperientialData({ cpId: targetCpId }); - - expect(data).toHaveProperty("translations"); - expect(data.translations).toHaveLength(2); - expect(data.translations).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: 11, - media_id: 10, - translation: "translation", - language: "en", - }), - expect.objectContaining({ - id: 22, - media_id: 20, - translation: "translation", - language: "it", - }), - ]) - ); - }); -}); diff --git a/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.ts b/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.ts deleted file mode 100644 index 4eb988f7a..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/getExperientialData/index.ts +++ /dev/null @@ -1,120 +0,0 @@ -import { tryber } from "@src/features/database"; - -interface TaggingToolData { - userTasksMedia?: any[]; - observations?: any[]; - tags?: any[]; - tagTypes?: any[]; - tagLinks?: any[]; - transcripts?: any[]; - translations?: any[]; -} - -async function getExperientialData({ - cpId, -}: { - cpId: number; -}): Promise { - const res: TaggingToolData = {}; - - // Campaign tasks - const campaignTasksIds = await tryber.tables.WpAppqCampaignTask.do() - .select("id") - .where("campaign_id", cpId); - - // User tasks media - const videos = await tryber.tables.WpAppqUserTaskMedia.do() - .select() - .whereIn( - "campaign_task_id", - campaignTasksIds.map((ct) => ct.id) - ); - - if (videos.length > 0) { - res.userTasksMedia = videos; - - // Media Observations - const observations = await tryber.tables.WpAppqUsecaseMediaObservations.do() - .select("wp_appq_usecase_media_observations.*") - .join( - "wp_appq_user_task_media", - "wp_appq_usecase_media_observations.media_id", - "wp_appq_user_task_media.id" - ) - .join( - "wp_appq_campaign_task", - "wp_appq_user_task_media.campaign_task_id", - "wp_appq_campaign_task.id" - ) - .where("wp_appq_campaign_task.campaign_id", cpId) - .groupBy("wp_appq_usecase_media_observations.id"); - - if (observations.length > 0) { - res.observations = observations; - } - - // Tag groups - const tagTypes = await tryber.tables.WpAppqUsecaseMediaTagType.do() - .select() - .where("campaign_id", cpId); - - if (tagTypes.length > 0) { - res.tagTypes = tagTypes; - } - - // Tags - const tags = await tryber.tables.WpAppqUsecaseMediaObservationsTags.do() - .select("wp_appq_usecase_media_observations_tags.*") - .join( - "wp_appq_usecase_media_tag_type", - "wp_appq_usecase_media_tag_type.id", - "wp_appq_usecase_media_observations_tags.type" - ) - .where("wp_appq_usecase_media_tag_type.campaign_id", cpId) - .groupBy("wp_appq_usecase_media_observations_tags.id"); - - if (tags.length > 0) { - res.tags = tags; - } - - // Tag links - const tagLinks = - await tryber.tables.WpAppqUsecaseMediaObservationsTagsLink.do() - .select("wp_appq_usecase_media_observations_tags_link.*") - .whereIn( - "tag_id", - tags.map((tag) => tag.id) - ) - .groupBy("wp_appq_usecase_media_observations_tags_link.id"); - - if (tagLinks.length > 0) { - res.tagLinks = tagLinks; - } - - // Transcripts - const transcripts = await tryber.tables.MediaTranscripts.do() - .select() - .whereIn( - "media_id", - videos.map((video) => video.id) - ); - if (transcripts.length > 0) { - res.transcripts = transcripts; - - // Translations - const translations = await tryber.tables.MediaTranscriptsTranslations.do() - .select() - .whereIn( - "media_id", - videos.map((video) => video.id) - ); - if (translations.length > 0) { - res.translations = translations; - } - } - } - - return res; -} - -export { getExperientialData }; diff --git a/src/routes/customers/_post/createDemoEnvironment/index.ts b/src/routes/customers/_post/createDemoEnvironment/index.ts index 7af953dc4..21073d22c 100644 --- a/src/routes/customers/_post/createDemoEnvironment/index.ts +++ b/src/routes/customers/_post/createDemoEnvironment/index.ts @@ -1,8 +1,3 @@ -import { createCpExperiential } from "./createCpExperiential"; -import { createCpFunctional } from "./createCpFunctional"; -import { createDemoProject } from "./createDemoProject"; -import { workspaceExist } from "./workspaceExist"; - async function createDemoEnvironment({ workspaceId }: { workspaceId: number }) { const sourceCpIdFunctional = 7916; const sourceCpIdExperiential = 7961; diff --git a/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.spec.ts b/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.spec.ts deleted file mode 100644 index 3caf1de53..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.spec.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { workspaceExist } from "./index"; -import { tryber } from "../db/database"; -describe("workspaceExist", () => { - beforeAll(async () => { - await tryber.tables.WpAppqCustomer.do().insert([ - { - id: 10, - company: "Company Name", - pm_id: 11111, - }, - ]); - }); - afterAll(async () => { - await tryber.tables.WpAppqCustomer.do().delete(); - }); - - it("should return true if workspace exist", async () => { - const result = await workspaceExist({ workspaceId: 10 }); - expect(result).toBe(true); - }); - it("should return false if workspace does not exist", async () => { - const result = await workspaceExist({ workspaceId: 20 }); - expect(result).toBe(false); - }); -}); diff --git a/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.ts b/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.ts deleted file mode 100644 index 4887a65c5..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/workspaceExist/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { tryber } from "@src/features/database"; - -async function workspaceExist({ - workspaceId, -}: { - workspaceId: number; -}): Promise { - const workspace = await tryber.tables.WpAppqCustomer.do() - .select("id") - .first() - .where("id", workspaceId); - - return workspace ? true : false; -} - -export { workspaceExist }; diff --git a/src/routes/customers/_post/index.ts b/src/routes/customers/_post/index.ts index 7dd0b6bc0..e3dfa5d04 100644 --- a/src/routes/customers/_post/index.ts +++ b/src/routes/customers/_post/index.ts @@ -28,9 +28,9 @@ class RouteItem extends UserRoute<{ protected async prepare(): Promise { const customer = await this.createCustomer(); - if (customer && customer.id) { - await createDemoEnvironment({ workspaceId: customer.id }); - } + // if (customer && customer.id) { + // await createDemoEnvironment({ workspaceId: customer.id }); + // } return this.setSuccess(201, customer); } @@ -39,7 +39,7 @@ class RouteItem extends UserRoute<{ const customer = await tryber.tables.WpAppqCustomer.do() .insert({ company: this.getBody().name, - pm_id: 0, + pm_id: this.getTesterId(), }) .returning("id"); const id = customer[0].id ?? customer[0]; From 17abb226028aa778de838b43e2b6da47eae7f35d Mon Sep 17 00:00:00 2001 From: sinatragianpaolo Date: Tue, 26 Nov 2024 17:57:57 +0100 Subject: [PATCH 3/9] refactor: Update config.ts to include unguessApi options - Added unguessApi object to the config file - Added basePath, username, and password properties to the unguessApi object - These properties are used for authentication with the Unguess API createDemoEnvironment function removed - Removed the createDemoEnvironment function from the createDemoEnvironment/index.ts file - This function was no longer being used and was causing unnecessary complexity in the code unguessPostCustomer function added - Added the unguessPostCustomer function to the unguessPostCustomer/index.ts file - This function is responsible for authenticating with the Unguess API and posting customer data - It takes the userId and company as parameters and returns the id and name of the created customer --- src/config.ts | 10 +++ .../_post/createDemoEnvironment/index.ts | 44 ------------- src/routes/customers/_post/index.spec.ts | 30 ++++++++- src/routes/customers/_post/index.ts | 26 ++------ .../_post/unguessPostCustomer/index.ts | 63 +++++++++++++++++++ 5 files changed, 107 insertions(+), 66 deletions(-) delete mode 100644 src/routes/customers/_post/createDemoEnvironment/index.ts create mode 100644 src/routes/customers/_post/unguessPostCustomer/index.ts diff --git a/src/config.ts b/src/config.ts index c34fc60fb..a244e0c17 100644 --- a/src/config.ts +++ b/src/config.ts @@ -32,6 +32,11 @@ const config: { private: string; }; GOOGLE_API_KEY: string; + unguessApi?: { + basePath?: string; + username?: string; + password?: string; + }; } = { port: process.env.PORT || "3000", apiRoot: false, @@ -59,6 +64,11 @@ const config: { }, CROWD_URL: process.env.CROWD_URL || "https://tryber.me/", GOOGLE_API_KEY: process.env.GOOGLE_API_KEY || "", + unguessApi: { + basePath: process.env.UNGUESS_API_ROOT || "", + username: process.env.UNGUESS_API_USERNAME || "", + password: process.env.UNGUESS_API_PASSWORD || "", + }, }; if (process.env.SSL_CHAIN && process.env.SSL_PRIVATE) { diff --git a/src/routes/customers/_post/createDemoEnvironment/index.ts b/src/routes/customers/_post/createDemoEnvironment/index.ts deleted file mode 100644 index 21073d22c..000000000 --- a/src/routes/customers/_post/createDemoEnvironment/index.ts +++ /dev/null @@ -1,44 +0,0 @@ -async function createDemoEnvironment({ workspaceId }: { workspaceId: number }) { - const sourceCpIdFunctional = 7916; - const sourceCpIdExperiential = 7961; - - if (await workspaceExist({ workspaceId })) { - const { projectId } = await createDemoProject({ workspaceId }); - if (!projectId) { - console.log("Error creating project"); - return; - } - console.log("Project created"); - - const { cpIdFunctional } = await createCpFunctional({ - projectId, - sourceCpId: sourceCpIdFunctional, - }); - if (!cpIdFunctional) { - console.log("Error creating Functional CP"); - return; - } - console.log( - "Functional CP created from campaignId: ", - sourceCpIdFunctional - ); - - const { cpIdExperiential } = await createCpExperiential({ - projectId, - sourceCpId: sourceCpIdExperiential, - }); - if (!cpIdExperiential) { - console.log("Error creating Experiential CP"); - return; - } - console.log( - "Experiential CP created from campaignId: ", - sourceCpIdExperiential - ); - } else { - console.log("Workspace not found"); - return; - } -} - -export { createDemoEnvironment }; diff --git a/src/routes/customers/_post/index.spec.ts b/src/routes/customers/_post/index.spec.ts index 7ebe878c2..1d997f6ff 100644 --- a/src/routes/customers/_post/index.spec.ts +++ b/src/routes/customers/_post/index.spec.ts @@ -3,8 +3,36 @@ import { tryber } from "@src/features/database"; import request from "supertest"; describe("POST /customers", () => { + beforeEach(async () => { + jest.mock("axios"); + const axios = require("axios"); + axios.post.mockImplementation(async (url: string) => { + if (url.includes("authenticate")) { + return { + data: { + token: "token", + }, + }; + } else if (url.includes("workspaces")) { + const newCustomer = await tryber.tables.WpAppqCustomer.do() + .insert({ + company: "New Customer", + pm_id: 1, + }) + .returning("id"); + return { + data: { + id: newCustomer[0].id, + name: "New Customer", + }, + }; + } + }); + }); + afterEach(async () => { await tryber.tables.WpAppqCustomer.do().delete(); + jest.clearAllMocks(); }); it("Should answer 403 if not logged in", () => { @@ -54,7 +82,7 @@ describe("POST /customers", () => { const customers = getResponse.body; expect(customers).toHaveLength(1); - expect(customers[0].id).toBe(id); expect(customers[0].name).toBe(name); + expect(customers[0].id).toBe(id); }); }); diff --git a/src/routes/customers/_post/index.ts b/src/routes/customers/_post/index.ts index e3dfa5d04..c048e7dac 100644 --- a/src/routes/customers/_post/index.ts +++ b/src/routes/customers/_post/index.ts @@ -1,9 +1,8 @@ /** OPENAPI-CLASS : post-customers */ import OpenapiError from "@src/features/OpenapiError"; -import { tryber } from "@src/features/database"; import UserRoute from "@src/features/routes/UserRoute"; -import { createDemoEnvironment } from "./createDemoEnvironment"; +import { unguessPostCustomer } from "./unguessPostCustomer"; class RouteItem extends UserRoute<{ response: StoplightOperations["post-customers"]["responses"]["200"]["content"]["application/json"]; @@ -27,28 +26,13 @@ class RouteItem extends UserRoute<{ } protected async prepare(): Promise { - const customer = await this.createCustomer(); - // if (customer && customer.id) { - // await createDemoEnvironment({ workspaceId: customer.id }); - // } + const customer = await unguessPostCustomer({ + company: this.getBody().name, + userId: this.getTesterId(), + }); return this.setSuccess(201, customer); } - - private async createCustomer() { - const customer = await tryber.tables.WpAppqCustomer.do() - .insert({ - company: this.getBody().name, - pm_id: this.getTesterId(), - }) - .returning("id"); - const id = customer[0].id ?? customer[0]; - - return { - id: id, - name: this.getBody().name, - }; - } } export default RouteItem; diff --git a/src/routes/customers/_post/unguessPostCustomer/index.ts b/src/routes/customers/_post/unguessPostCustomer/index.ts new file mode 100644 index 000000000..24ba9acb3 --- /dev/null +++ b/src/routes/customers/_post/unguessPostCustomer/index.ts @@ -0,0 +1,63 @@ +import axios from "axios"; +import config from "@src/config"; +const { basePath, username, password } = config.unguessApi || {}; + +async function authenticateUnguess(): Promise { + try { + const response = await axios.post(`${basePath}/authenticate`, { + username, + password, + }); + + if (response.data && response.data.token) { + return response.data.token; + } else { + throw new Error("Authentication failed: Token not found"); + } + } catch (error) { + console.error("Error authenticating with Unguess API:", error); + throw error; + } +} + +async function postCustomerUnguess( + token: string, + company: string, + userId: number +): Promise<{ id: number; name: string }> { + try { + const response = await axios.post( + `${basePath}/workspaces`, + { company, pm_id: userId }, + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); + return response.data; + } catch (error) { + console.error("Error posting customer data", error); + throw error; + } +} + +async function unguessPostCustomer({ + userId, + company, +}: { + userId: number; + company: string; +}): Promise<{ id: number; name: string } | undefined> { + try { + const token = await authenticateUnguess(); + + const customer = await postCustomerUnguess(token, company, userId); + + return { id: customer.id, name: customer.name }; + } catch (error) { + console.error("Error in callUnguessPostCustomer:", error); + } +} + +export { unguessPostCustomer }; From a3649b210392e3f19626380e050a433f96a072c1 Mon Sep 17 00:00:00 2001 From: sinatragianpaolo Date: Wed, 27 Nov 2024 15:13:02 +0100 Subject: [PATCH 4/9] use fetch insteadof axios on postCustomer --- src/routes/customers/_post/index.spec.ts | 57 +++++++++++-------- .../_post/unguessPostCustomer/index.ts | 47 +++++++++------ 2 files changed, 62 insertions(+), 42 deletions(-) diff --git a/src/routes/customers/_post/index.spec.ts b/src/routes/customers/_post/index.spec.ts index 1d997f6ff..b4c0878ad 100644 --- a/src/routes/customers/_post/index.spec.ts +++ b/src/routes/customers/_post/index.spec.ts @@ -4,35 +4,39 @@ import request from "supertest"; describe("POST /customers", () => { beforeEach(async () => { - jest.mock("axios"); - const axios = require("axios"); - axios.post.mockImplementation(async (url: string) => { - if (url.includes("authenticate")) { - return { - data: { - token: "token", - }, - }; - } else if (url.includes("workspaces")) { - const newCustomer = await tryber.tables.WpAppqCustomer.do() - .insert({ - company: "New Customer", - pm_id: 1, - }) - .returning("id"); - return { - data: { - id: newCustomer[0].id, - name: "New Customer", - }, - }; - } - }); + jest + .spyOn(global, "fetch") + .mockImplementation( + async (url: RequestInfo | URL, options?: RequestInit) => { + if (typeof url === "string" && url.includes("authenticate")) { + return Promise.resolve({ + ok: true, + json: async () => ({ token: "token" }), + } as Response); + } else if (typeof url === "string" && url.includes("workspaces")) { + const newCustomer = await tryber.tables.WpAppqCustomer.do() + .insert({ + company: "New Customer", + pm_id: 1, + }) + .returning("id"); + + return Promise.resolve({ + ok: true, + json: async () => ({ + id: newCustomer[0].id, + name: "New Customer", + }), + } as Response); + } + return Promise.reject(new Error("Invalid URL")); + } + ); }); afterEach(async () => { await tryber.tables.WpAppqCustomer.do().delete(); - jest.clearAllMocks(); + jest.restoreAllMocks(); }); it("Should answer 403 if not logged in", () => { @@ -41,6 +45,7 @@ describe("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") @@ -48,6 +53,7 @@ describe("POST /customers", () => { .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") @@ -55,6 +61,7 @@ describe("POST /customers", () => { .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") diff --git a/src/routes/customers/_post/unguessPostCustomer/index.ts b/src/routes/customers/_post/unguessPostCustomer/index.ts index 24ba9acb3..16d79d6b5 100644 --- a/src/routes/customers/_post/unguessPostCustomer/index.ts +++ b/src/routes/customers/_post/unguessPostCustomer/index.ts @@ -1,16 +1,24 @@ -import axios from "axios"; import config from "@src/config"; const { basePath, username, password } = config.unguessApi || {}; async function authenticateUnguess(): Promise { try { - const response = await axios.post(`${basePath}/authenticate`, { - username, - password, + const response = await fetch(`${basePath}/authenticate`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ username, password }), }); - if (response.data && response.data.token) { - return response.data.token; + if (!response.ok) { + throw new Error(`Authentication failed: ${response.statusText}`); + } + + const data = await response.json(); + + if (data && data.token) { + return data.token; } else { throw new Error("Authentication failed: Token not found"); } @@ -26,18 +34,23 @@ async function postCustomerUnguess( userId: number ): Promise<{ id: number; name: string }> { try { - const response = await axios.post( - `${basePath}/workspaces`, - { company, pm_id: userId }, - { - headers: { - Authorization: `Bearer ${token}`, - }, - } - ); - return response.data; + const response = await fetch(`${basePath}/workspaces`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ company, pm_id: userId }), + }); + + if (!response.ok) { + throw new Error(`Error posting customer data: ${response.statusText}`); + } + + const data = await response.json(); + return data; } catch (error) { - console.error("Error posting customer data", error); + console.error("Error posting customer data:", error); throw error; } } From abc0d66c3d255da0609dc13666e49158191c8a55 Mon Sep 17 00:00:00 2001 From: sinatragianpaolo Date: Wed, 27 Nov 2024 15:34:18 +0100 Subject: [PATCH 5/9] refactor: Replace axios with fetch in postCustomer method Refactor the postCustomer method in the Unguess class to use the fetch API instead of axios for making HTTP requests. This change improves the code's performance and reduces the number of dependencies. --- src/features/class/Unguess.ts | 85 +++++++++++++++++++ src/routes/customers/_post/index.ts | 25 ++++-- .../_post/unguessPostCustomer/index.ts | 76 ----------------- 3 files changed, 105 insertions(+), 81 deletions(-) create mode 100644 src/features/class/Unguess.ts delete mode 100644 src/routes/customers/_post/unguessPostCustomer/index.ts diff --git a/src/features/class/Unguess.ts b/src/features/class/Unguess.ts new file mode 100644 index 000000000..8a66cfaec --- /dev/null +++ b/src/features/class/Unguess.ts @@ -0,0 +1,85 @@ +class Unguess { + private baseUrl: string; + private username: string; + private password: string; + + constructor(baseUrl: string, username: string, password: string) { + this.baseUrl = baseUrl; + this.username = username; + this.password = password; + } + + /** + * Private method to fetch a token for API requests + */ + private async getToken(): Promise { + const response = await fetch(`${this.baseUrl}/authenticate`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + username: this.username, + password: this.password, + }), + }); + + if (!response.ok) { + throw new Error("Failed to authenticate: " + response.statusText); + } + + const data = await response.json(); + if (!data.token) { + throw new Error("Authentication failed: Token not found"); + } + + return data.token; + } + + /** + * Private method to perform authenticated POST requests + */ + private async authPost( + path: string, + body: Record + ): Promise { + const token = await this.getToken(); + const response = await fetch(`${this.baseUrl}${path}`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify(body), + }); + + if (!response.ok) { + throw new Error(`Failed to post to ${path}: ${response.statusText}`); + } + + return response.json(); + } + + /** + * Public method to post a new customer + */ + public async postCustomer({ + userId, + name, + }: { + userId: number; + name: string; + }): Promise<{ id: number; name: string }> { + const body = { + company: name, + pm_id: userId, + }; + const result = await this.authPost("/workspaces", body); + return { + id: result.id, + name: result.name, + }; + } +} + +export default Unguess; diff --git a/src/routes/customers/_post/index.ts b/src/routes/customers/_post/index.ts index c048e7dac..2ad21216d 100644 --- a/src/routes/customers/_post/index.ts +++ b/src/routes/customers/_post/index.ts @@ -2,7 +2,8 @@ import OpenapiError from "@src/features/OpenapiError"; import UserRoute from "@src/features/routes/UserRoute"; -import { unguessPostCustomer } from "./unguessPostCustomer"; +import Unguess from "@src/features/class/Unguess"; +import config from "@src/config"; class RouteItem extends UserRoute<{ response: StoplightOperations["post-customers"]["responses"]["200"]["content"]["application/json"]; @@ -26,13 +27,27 @@ class RouteItem extends UserRoute<{ } protected async prepare(): Promise { - const customer = await unguessPostCustomer({ - company: this.getBody().name, - userId: this.getTesterId(), - }); + const customer = await this.postCustomerUnguessApi(); return this.setSuccess(201, customer); } + + private async postCustomerUnguessApi() { + const { basePath, username, password } = config.unguessApi || {}; + + const unguess = new Unguess(basePath || "", username || "", password || ""); + + try { + const customer = await unguess.postCustomer({ + userId: this.getTesterId(), + name: this.getBody().name, + }); + console.log("Customer created:", customer); + return customer; + } catch (error) { + console.error("Error creating customer:", error); + } + } } export default RouteItem; diff --git a/src/routes/customers/_post/unguessPostCustomer/index.ts b/src/routes/customers/_post/unguessPostCustomer/index.ts deleted file mode 100644 index 16d79d6b5..000000000 --- a/src/routes/customers/_post/unguessPostCustomer/index.ts +++ /dev/null @@ -1,76 +0,0 @@ -import config from "@src/config"; -const { basePath, username, password } = config.unguessApi || {}; - -async function authenticateUnguess(): Promise { - try { - const response = await fetch(`${basePath}/authenticate`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ username, password }), - }); - - if (!response.ok) { - throw new Error(`Authentication failed: ${response.statusText}`); - } - - const data = await response.json(); - - if (data && data.token) { - return data.token; - } else { - throw new Error("Authentication failed: Token not found"); - } - } catch (error) { - console.error("Error authenticating with Unguess API:", error); - throw error; - } -} - -async function postCustomerUnguess( - token: string, - company: string, - userId: number -): Promise<{ id: number; name: string }> { - try { - const response = await fetch(`${basePath}/workspaces`, { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify({ company, pm_id: userId }), - }); - - if (!response.ok) { - throw new Error(`Error posting customer data: ${response.statusText}`); - } - - const data = await response.json(); - return data; - } catch (error) { - console.error("Error posting customer data:", error); - throw error; - } -} - -async function unguessPostCustomer({ - userId, - company, -}: { - userId: number; - company: string; -}): Promise<{ id: number; name: string } | undefined> { - try { - const token = await authenticateUnguess(); - - const customer = await postCustomerUnguess(token, company, userId); - - return { id: customer.id, name: customer.name }; - } catch (error) { - console.error("Error in callUnguessPostCustomer:", error); - } -} - -export { unguessPostCustomer }; From 05454053fa0e456f04188fd000e466e46fa46670 Mon Sep 17 00:00:00 2001 From: sinatragianpaolo Date: Wed, 27 Nov 2024 15:40:55 +0100 Subject: [PATCH 6/9] refactor: Remove console.log statement in postCustomer method --- src/routes/customers/_post/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/routes/customers/_post/index.ts b/src/routes/customers/_post/index.ts index 2ad21216d..c4ffc2f61 100644 --- a/src/routes/customers/_post/index.ts +++ b/src/routes/customers/_post/index.ts @@ -42,7 +42,6 @@ class RouteItem extends UserRoute<{ userId: this.getTesterId(), name: this.getBody().name, }); - console.log("Customer created:", customer); return customer; } catch (error) { console.error("Error creating customer:", error); From 4ed590ae51bd7bc21d1d545c50273f28a25ac143 Mon Sep 17 00:00:00 2001 From: sinatragianpaolo Date: Mon, 2 Dec 2024 12:51:58 +0100 Subject: [PATCH 7/9] refactor: Update Unguess class constructor to use config values for baseUrl, username, and password --- src/features/class/Unguess.ts | 11 +++++++---- src/routes/customers/_post/index.ts | 6 ++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/features/class/Unguess.ts b/src/features/class/Unguess.ts index 8a66cfaec..e262ed454 100644 --- a/src/features/class/Unguess.ts +++ b/src/features/class/Unguess.ts @@ -1,12 +1,15 @@ +import config from "@src/config"; + class Unguess { private baseUrl: string; private username: string; private password: string; - constructor(baseUrl: string, username: string, password: string) { - this.baseUrl = baseUrl; - this.username = username; - this.password = password; + constructor() { + const { basePath, username, password } = config.unguessApi || {}; + this.baseUrl = basePath || ""; + this.username = username || ""; + this.password = password || ""; } /** diff --git a/src/routes/customers/_post/index.ts b/src/routes/customers/_post/index.ts index c4ffc2f61..58919fd9f 100644 --- a/src/routes/customers/_post/index.ts +++ b/src/routes/customers/_post/index.ts @@ -3,7 +3,6 @@ import OpenapiError from "@src/features/OpenapiError"; import UserRoute from "@src/features/routes/UserRoute"; import Unguess from "@src/features/class/Unguess"; -import config from "@src/config"; class RouteItem extends UserRoute<{ response: StoplightOperations["post-customers"]["responses"]["200"]["content"]["application/json"]; @@ -19,6 +18,7 @@ class RouteItem extends UserRoute<{ this.setError(403, new OpenapiError("You are not authorized to do this")); return false; } + return true; } @@ -33,9 +33,7 @@ class RouteItem extends UserRoute<{ } private async postCustomerUnguessApi() { - const { basePath, username, password } = config.unguessApi || {}; - - const unguess = new Unguess(basePath || "", username || "", password || ""); + const unguess = new Unguess(); try { const customer = await unguess.postCustomer({ From 355ff419860c07ecf7524961353d4c7eefe841df Mon Sep 17 00:00:00 2001 From: sinatragianpaolo Date: Mon, 2 Dec 2024 12:54:06 +0100 Subject: [PATCH 8/9] refactor: Update deployment configuration to include Unguess API credentials --- deployment/after-install.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/deployment/after-install.sh b/deployment/after-install.sh index 12b06657a..f1a3da8c5 100644 --- a/deployment/after-install.sh +++ b/deployment/after-install.sh @@ -45,6 +45,9 @@ services: environment: PORT: 80 API_ROOT: ${API_ROOT} + UNGUESS_API_ROOT: '${UNGUESS_API_ROOT}' + UNGUESS_API_USERNAME: ${UNGUESS_API_USERNAME} + UNGUESS_API_PASSWORD: ${UNGUESS_API_PASSWORD} SENDGRID_KEY: '${SENDGRID_KEY}' DEFAULT_SENDER_MAIL: '${DEFAULT_SENDER_MAIL}' DEFAULT_SENDER_NAME: '${DEFAULT_SENDER_NAME}' From 3ff61a15d80ed3c66e4127ee0557763907585d50 Mon Sep 17 00:00:00 2001 From: sinatragianpaolo Date: Mon, 2 Dec 2024 12:55:24 +0100 Subject: [PATCH 9/9] refactor: Update deployment configuration to include quoted Unguess API credentials --- deployment/after-install.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deployment/after-install.sh b/deployment/after-install.sh index f1a3da8c5..ec45cd2b1 100644 --- a/deployment/after-install.sh +++ b/deployment/after-install.sh @@ -46,8 +46,8 @@ services: PORT: 80 API_ROOT: ${API_ROOT} UNGUESS_API_ROOT: '${UNGUESS_API_ROOT}' - UNGUESS_API_USERNAME: ${UNGUESS_API_USERNAME} - UNGUESS_API_PASSWORD: ${UNGUESS_API_PASSWORD} + UNGUESS_API_USERNAME: '${UNGUESS_API_USERNAME}' + UNGUESS_API_PASSWORD: '${UNGUESS_API_PASSWORD}' SENDGRID_KEY: '${SENDGRID_KEY}' DEFAULT_SENDER_MAIL: '${DEFAULT_SENDER_MAIL}' DEFAULT_SENDER_NAME: '${DEFAULT_SENDER_NAME}'