Skip to content

Commit

Permalink
fix(api): Return correct workflow.origin (#6740)
Browse files Browse the repository at this point in the history
  • Loading branch information
SokratisVidros authored Oct 22, 2024
1 parent 4b91fdf commit d5acffd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export function toResponseWorkflowDto(
name: template.name,
workflowId: template.triggers[0].identifier,
description: template.description,
origin: template.origin || WorkflowOriginEnum.EXTERNAL,
type: template.type || ('MISSING-TYPE-ISSUE' as unknown as WorkflowTypeEnum),
origin: computeOrigin(template),
updatedAt: template.updatedAt || 'Missing Updated At',
createdAt: template.createdAt || 'Missing Create At',
status: WorkflowStatusEnum.ACTIVE,
Expand All @@ -57,8 +56,7 @@ function getSteps(template: NotificationTemplateEntity, controlValuesMap: { [p:

function toMinifiedWorkflowDto(template: NotificationTemplateEntity): WorkflowListResponseDto {
return {
origin: template.origin || WorkflowOriginEnum.EXTERNAL,
type: template.type || ('MISSING-TYPE-ISSUE' as unknown as WorkflowTypeEnum),
origin: computeOrigin(template),
_id: template._id,
name: template.name,
tags: template.tags,
Expand Down Expand Up @@ -96,3 +94,10 @@ function convertControls(step: NotificationStepEntity): ControlsSchema {
function buildStepTypeOverview(step: NotificationStepEntity): StepTypeEnum | undefined {
return step.template?.type;
}

function computeOrigin(template: NotificationTemplateEntity): WorkflowOriginEnum {
// Required to differentiate between old V1 and new workflows in an attempt to eliminate the need for type field
return template?.type === WorkflowTypeEnum.REGULAR
? WorkflowOriginEnum.NOVU_CLOUD_V1
: template.origin || WorkflowOriginEnum.EXTERNAL;
}
43 changes: 21 additions & 22 deletions apps/api/src/app/workflows-v2/workflow.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ describe('Workflow Controller E2E API Testing', () => {
});
expect(workflowSummaries).to.be.empty;
});

it('should not return workflows if offset is bigger than the amount of available workflows', async () => {
const uuid = generateUUID();
await create10Workflows(uuid);
Expand All @@ -149,6 +150,7 @@ describe('Workflow Controller E2E API Testing', () => {
expectedArraySize: 0,
});
});

it('should return all results within range', async () => {
const uuid = generateUUID();

Expand Down Expand Up @@ -222,26 +224,26 @@ async function createWorkflowAndValidate(nameSuffix: string = ''): Promise<Workf
const createWorkflowDto: CreateWorkflowDto = buildCreateWorkflowDto(nameSuffix);
const res = await session.testAgent.post(`${v2Prefix}/workflows`).send(createWorkflowDto);
const workflowResponseDto: WorkflowResponseDto = res.body.data;
expect(workflowResponseDto, JSON.stringify(res, null, 2)).to.be.ok;
expect(workflowResponseDto._id, JSON.stringify(res, null, 2)).to.be.ok;
expect(workflowResponseDto.updatedAt, JSON.stringify(res, null, 2)).to.be.ok;
expect(workflowResponseDto.createdAt, JSON.stringify(res, null, 2)).to.be.ok;
expect(workflowResponseDto.preferences, JSON.stringify(res, null, 2)).to.be.ok;
expect(workflowResponseDto.status, JSON.stringify(res, null, 2)).to.be.ok;
const errorMessageOnFailure = JSON.stringify(res, null, 2);
expect(workflowResponseDto, errorMessageOnFailure).to.be.ok;
expect(workflowResponseDto._id, errorMessageOnFailure).to.be.ok;
expect(workflowResponseDto.updatedAt, errorMessageOnFailure).to.be.ok;
expect(workflowResponseDto.createdAt, errorMessageOnFailure).to.be.ok;
expect(workflowResponseDto.preferences, errorMessageOnFailure).to.be.ok;
expect(workflowResponseDto.status, errorMessageOnFailure).to.be.ok;
expect(workflowResponseDto.origin, errorMessageOnFailure).to.be.eq('novu-cloud');
for (const step of workflowResponseDto.steps) {
expect(step.stepUuid, JSON.stringify(res, null, 2)).to.be.ok;
expect(step.slug, JSON.stringify(res, null, 2)).to.be.ok;
expect(step.stepUuid, errorMessageOnFailure).to.be.ok;
expect(step.slug, errorMessageOnFailure).to.be.ok;
}
expect(workflowResponseDto.steps, JSON.stringify(res, null, 2)).to.be.ok;
const createdWorkflowWithoutUpdateDate = removeFields(
workflowResponseDto,
'_id',
'origin',
'preferences',
'updatedAt',
'createdAt',
'status',
'type'
'status'
);
createdWorkflowWithoutUpdateDate.steps = createdWorkflowWithoutUpdateDate.steps.map((step) =>
removeFields(step, 'stepUuid', 'slug', 'controls', 'stepId')
Expand Down Expand Up @@ -348,8 +350,7 @@ function validateUpdatedWorkflowAndRemoveResponseFields(
'updatedAt',
'origin',
'_id',
'status',
'type'
'status'
);
const augmentedSteps: (StepUpdateDto | StepCreateDto)[] = [];

Expand Down Expand Up @@ -527,6 +528,7 @@ async function create10Workflows(prefix: string) {
await createWorkflowAndValidate(`${prefix}-ABC${i}`);
}
}

function removeFields<T>(obj: T, ...keysToRemove: (keyof T)[]): T {
const objCopy = JSON.parse(JSON.stringify(obj));
keysToRemove.forEach((key) => {
Expand All @@ -535,6 +537,7 @@ function removeFields<T>(obj: T, ...keysToRemove: (keyof T)[]): T {

return objCopy;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
interface ApiResponse {
req: {
Expand Down Expand Up @@ -570,15 +573,19 @@ interface ApiResponse {
status: number; // e.g., 400
text: string; // e.g., "{\"message\":\"Workflow not found with id: 66e929c6667852862a1e5145\",\"error\":\"Bad Request\",\"statusCode\":400}"
}

async function safeGet<T>(url: string): Promise<T> {
return (await safeRest(url, () => session.testAgent.get(url) as unknown as Promise<ApiResponse>)) as T;
}

async function safePut<T>(url: string, data: object): Promise<T> {
return (await safeRest(url, () => session.testAgent.put(url).send(data) as unknown as Promise<ApiResponse>)) as T;
}

async function safeDelete<T>(url: string): Promise<void> {
await safeRest(url, () => session.testAgent.delete(url) as unknown as Promise<ApiResponse>, 204);
}

function generateUUID(): string {
// Generate a random 4-byte hex string
const randomHex = () => randomBytes(2).toString('hex');
Expand Down Expand Up @@ -608,7 +615,6 @@ function convertResponseToUpdateDto(workflowCreated: WorkflowResponseDto): Updat
'updatedAt',
'_id',
'origin',
'type',
'status'
) as UpdateWorkflowDto;

Expand Down Expand Up @@ -639,14 +645,7 @@ function createStep(): StepCreateDto {

function buildUpdateRequest(workflowCreated: WorkflowResponseDto): UpdateWorkflowDto {
const steps = [createStep()];
const updateRequest = removeFields(
workflowCreated,
'updatedAt',
'_id',
'origin',
'status',
'type'
) as UpdateWorkflowDto;
const updateRequest = removeFields(workflowCreated, 'updatedAt', '_id', 'origin', 'status') as UpdateWorkflowDto;

return {
...updateRequest,
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/src/utils/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum WorkflowTypeEnum {

export enum WorkflowOriginEnum {
NOVU_CLOUD = 'novu-cloud',
NOVU_CLOUD_V1 = 'novu-cloud-v1',
EXTERNAL = 'external',
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ListWorkflowResponse = {

export type WorkflowListResponseDto = Pick<
WorkflowResponseDto,
'name' | 'tags' | 'updatedAt' | 'createdAt' | '_id' | 'status' | 'type' | 'origin'
'name' | 'tags' | 'updatedAt' | 'createdAt' | '_id' | 'status' | 'origin'
> & {
stepTypeOverviews: StepTypeEnum[];
};
Expand Down
2 changes: 0 additions & 2 deletions packages/shared/src/dto/workflows/workflow-response-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ export class WorkflowResponseDto extends WorkflowCommonsFields {
preferences: PreferencesResponseDto;

status: WorkflowStatusEnum;

type: WorkflowTypeEnum;
}
1 change: 1 addition & 0 deletions packages/shared/src/types/notification-templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export enum WorkflowTypeEnum {
*/
export enum WorkflowOriginEnum {
NOVU_CLOUD = 'novu-cloud',
NOVU_CLOUD_V1 = 'novu-cloud-v1',
EXTERNAL = 'external',
}
export * from './workflow-creation-source.enum';

0 comments on commit d5acffd

Please sign in to comment.