Skip to content

Commit

Permalink
fix(cards): fixed growthHack template duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
batamar committed Mar 21, 2023
1 parent 115ecaf commit db0c2d8
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions packages/plugin-cards-api/src/models/PipelineTemplates.ts
Expand Up @@ -13,21 +13,32 @@ interface IDoc {
type: string;
}

export const getDuplicatedStages = async (models: IModels, subdomain: string, {
templateId,
pipelineId,
type
}: {
templateId: string;
pipelineId?: string;
type?: string;
}) => {
const template = await models.PipelineTemplates.getPipelineTemplate(templateId);
export const getDuplicatedStages = async (
models: IModels,
subdomain: string,
{
templateId,
pipelineId,
type
}: {
templateId: string;
pipelineId?: string;
type?: string;
}
) => {
const template = await models.PipelineTemplates.getPipelineTemplate(
templateId
);

const stages: any[] = [];

for (const stage of template.stages) {
const duplicated = await sendFormsMessage({ subdomain, action: 'duplicate', data: stage.formId, isRPC: true });
const duplicated = await sendFormsMessage({
subdomain,
action: 'duplicate',
data: { formId: stage.formId },
isRPC: true
});

stages.push({
_id: Math.random().toString(),
Expand Down Expand Up @@ -57,7 +68,10 @@ export interface IPipelineTemplateModel
duplicatePipelineTemplate(_id: string): Promise<IPipelineTemplateDocument>;
}

export const loadPipelineTemplateClass = (models: IModels, subdomain: string) => {
export const loadPipelineTemplateClass = (
models: IModels,
subdomain: string
) => {
class PipelineTemplate {
/*
* Get a pipeline template
Expand Down Expand Up @@ -106,7 +120,9 @@ export const loadPipelineTemplateClass = (models: IModels, subdomain: string) =>
* Duplicate pipeline template
*/
public static async duplicatePipelineTemplate(_id: string) {
const pipelineTemplate = await models.PipelineTemplates.findOne({ _id }).lean();
const pipelineTemplate = await models.PipelineTemplates.findOne({
_id
}).lean();

if (!pipelineTemplate) {
throw new Error('Pipeline template not found');
Expand All @@ -122,7 +138,10 @@ export const loadPipelineTemplateClass = (models: IModels, subdomain: string) =>
templateId: pipelineTemplate._id
});

return models.PipelineTemplates.createPipelineTemplate(duplicated, stages);
return models.PipelineTemplates.createPipelineTemplate(
duplicated,
stages
);
}

/**
Expand All @@ -136,7 +155,11 @@ export const loadPipelineTemplateClass = (models: IModels, subdomain: string) =>
}

for (const stage of pipelineTemplate.stages) {
sendFormsMessage({ subdomain, action: 'removeForm', data: { formId: stage.formId } });
sendFormsMessage({
subdomain,
action: 'removeForm',
data: { formId: stage.formId }
});
}

return models.PipelineTemplates.deleteOne({ _id });
Expand All @@ -146,4 +169,4 @@ export const loadPipelineTemplateClass = (models: IModels, subdomain: string) =>
pipelineTemplateSchema.loadClass(PipelineTemplate);

return pipelineTemplateSchema;
};
};

0 comments on commit db0c2d8

Please sign in to comment.