Skip to content

Commit

Permalink
feat(api): revert to full slug (#6756)
Browse files Browse the repository at this point in the history
  • Loading branch information
djabarovgeorge authored Oct 25, 2024
1 parent 74a2e9a commit 3398ad7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PreferencesResponseDto,
PreferencesTypeEnum,
ShortIsPrefixEnum,
Slug,
StepResponseDto,
StepTypeEnum,
WorkflowListResponseDto,
Expand All @@ -13,9 +14,11 @@ import {
WorkflowTypeEnum,
} from '@novu/shared';
import { ControlValuesEntity, NotificationStepEntity, NotificationTemplateEntity } from '@novu/dal';
import { GetPreferencesResponseDto } from '@novu/application-generic';
import { GetPreferencesResponseDto, slugifyName } from '@novu/application-generic';
import { encodeBase62 } from '../../shared/helpers';

const SLUG_DELIMITER = '_';

export function toResponseWorkflowDto(
template: NotificationTemplateEntity,
preferences: GetPreferencesResponseDto | undefined,
Expand All @@ -29,7 +32,7 @@ export function toResponseWorkflowDto(

return {
_id: template._id,
slug: `${ShortIsPrefixEnum.WORKFLOW}${encodeBase62(template._id)}`,
slug: buildSlug(workflowName, ShortIsPrefixEnum.WORKFLOW, template._id),
workflowId: template.triggers[0].identifier,
name: workflowName,
tags: template.tags,
Expand Down Expand Up @@ -63,7 +66,7 @@ function toMinifiedWorkflowDto(template: NotificationTemplateEntity): WorkflowLi

return {
_id: template._id,
slug: `${ShortIsPrefixEnum.WORKFLOW}${encodeBase62(template._id)}`,
slug: buildSlug(workflowName, ShortIsPrefixEnum.WORKFLOW, template._id),
name: workflowName,
origin: computeOrigin(template),
tags: template.tags,
Expand All @@ -83,7 +86,7 @@ function toStepResponseDto(step: NotificationStepEntity): StepResponseDto {

return {
_id: step._templateId,
slug: `${ShortIsPrefixEnum.STEP}${encodeBase62(step._templateId)}`,
slug: buildSlug(stepName, ShortIsPrefixEnum.STEP, step._templateId),
name: stepName,
stepId: step.stepId || 'Missing Step Id',
type: step.template?.type || StepTypeEnum.EMAIL,
Expand All @@ -92,6 +95,14 @@ function toStepResponseDto(step: NotificationStepEntity): StepResponseDto {
} satisfies StepResponseDto;
}

/**
* Builds a slug for a step based on the step name, the short prefix and the internal ID.
* @returns The slug for the entity, example: slug: "workflow-name_wf_AbC1Xyz9KlmNOpQr"
*/
function buildSlug(entityName: string, shortIsPrefix: ShortIsPrefixEnum, internalId: string): Slug {
return `${slugifyName(entityName)}${SLUG_DELIMITER}${shortIsPrefix}${encodeBase62(internalId)}`;
}

function convertControls(step: NotificationStepEntity): ControlsSchema {
if (step.template?.controls) {
return { schema: step.template.controls.schema };
Expand Down
8 changes: 6 additions & 2 deletions apps/api/src/app/workflows-v2/workflow.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ function updateStepId(step: StepResponseDto): Partial<StepResponseDto> {
return {
...rest,
...(step._id && step.name ? { stepId: slugifyName(step.name) } : {}),
...(step.name && step._id ? { slug: `${ShortIsPrefixEnum.STEP}${encodeBase62(step._id)}` } : {}),
...(step.name && step._id
? { slug: `${slugifyName(step.name)}_${ShortIsPrefixEnum.STEP}${encodeBase62(step._id)}` }
: {}),
};
}

Expand Down Expand Up @@ -434,7 +436,9 @@ async function updateWorkflowAndValidate(
);
const expectedUpdateRequest = {
...updateRequest,
slug: `${ShortIsPrefixEnum.WORKFLOW}${encodeBase62(workflowInternalId || workflowRequestId)}`,
slug: `${slugifyName(updateRequest.name)}_${ShortIsPrefixEnum.WORKFLOW}${encodeBase62(
workflowInternalId || workflowRequestId
)}`,
steps: updateRequest.steps.map(updateStepId),
};
expect(updatedWorkflowWithResponseFieldsRemoved, 'workflow after update does not match as expected').to.deep.equal(
Expand Down
4 changes: 3 additions & 1 deletion packages/shared/src/types/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ export type DeepPartial<T> = T extends object

export type Base62Id = string;

export type WorkflowName = string;

export enum ShortIsPrefixEnum {
WORKFLOW = 'wf_',
STEP = 'stp_',
}

export type Slug = `${ShortIsPrefixEnum}${Base62Id}`;
export type Slug = `${WorkflowName}_${ShortIsPrefixEnum}${Base62Id}`;

0 comments on commit 3398ad7

Please sign in to comment.