Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
romainminaud committed Oct 23, 2023
2 parents 217819d + bf2e7fc commit 12fa870
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type express from 'express';
import { Container } from 'typedi';
import type { FindOptionsWhere } from 'typeorm';
import { In } from 'typeorm';
import { v4 as uuid } from 'uuid';

import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import config from '@/config';
Expand Down Expand Up @@ -36,6 +37,7 @@ export = {
const workflow = req.body;

workflow.active = false;
workflow.versionId = uuid();

await replaceInvalidCredentials(workflow);

Expand All @@ -45,6 +47,14 @@ export = {

const createdWorkflow = await createWorkflow(workflow, req.user, role);

if (isWorkflowHistoryLicensed()) {
await Container.get(WorkflowHistoryService).saveVersion(
req.user,
createdWorkflow,
createdWorkflow.id,
);
}

await Container.get(ExternalHooks).run('workflow.afterCreate', [createdWorkflow]);
void Container.get(InternalHooks).onWorkflowCreated(req.user, createdWorkflow, true);

Expand Down Expand Up @@ -151,6 +161,7 @@ export = {
const updateData = new WorkflowEntity();
Object.assign(updateData, req.body);
updateData.id = id;
updateData.versionId = uuid();

const sharedWorkflow = await getSharedWorkflow(req.user, id);

Expand Down Expand Up @@ -179,10 +190,6 @@ export = {
}
}

if (isWorkflowHistoryLicensed()) {
await Container.get(WorkflowHistoryService).saveVersion(req.user, sharedWorkflow.workflow);
}

if (sharedWorkflow.workflow.active) {
try {
await workflowRunner.add(sharedWorkflow.workflowId, 'update');
Expand All @@ -195,6 +202,14 @@ export = {

const updatedWorkflow = await getWorkflowById(sharedWorkflow.workflowId);

if (isWorkflowHistoryLicensed() && updatedWorkflow) {
await Container.get(WorkflowHistoryService).saveVersion(
req.user,
updatedWorkflow,
sharedWorkflow.workflowId,
);
}

await Container.get(ExternalHooks).run('workflow.afterUpdate', [updateData]);
void Container.get(InternalHooks).onWorkflowSaved(req.user, updateData, true);

Expand Down
18 changes: 13 additions & 5 deletions packages/cli/src/services/frontend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ import {
getWorkflowHistoryPruneTime,
} from '@/workflows/workflowHistory/workflowHistoryHelper.ee';
import { UserManagementMailer } from '@/UserManagement/email';
import type { CommunityPackagesService } from '@/services/communityPackages.service';

@Service()
export class FrontendService {
settings: IN8nUISettings;

private communityPackagesService?: CommunityPackagesService;

constructor(
private readonly loadNodesAndCredentials: LoadNodesAndCredentials,
private readonly credentialTypes: CredentialTypes,
Expand All @@ -44,6 +47,13 @@ export class FrontendService {
private readonly instanceSettings: InstanceSettings,
) {
this.initSettings();

if (config.getEnv('nodes.communityPackages.enabled')) {
// eslint-disable-next-line @typescript-eslint/naming-convention
void import('@/services/communityPackages.service').then(({ CommunityPackagesService }) => {
this.communityPackagesService = Container.get(CommunityPackagesService);
});
}
}

private initSettings() {
Expand Down Expand Up @@ -198,7 +208,7 @@ export class FrontendService {
this.writeStaticJSON('credentials', credentials);
}

async getSettings(): Promise<IN8nUISettings> {
getSettings(): IN8nUISettings {
const restEndpoint = config.getEnv('endpoints.rest');

// Update all urls, in case `WEBHOOK_URL` was updated by `--tunnel`
Expand Down Expand Up @@ -275,10 +285,8 @@ export class FrontendService {
});
}

if (config.getEnv('nodes.communityPackages.enabled')) {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { CommunityPackagesService } = await import('@/services/communityPackages.service');
this.settings.missingPackages = Container.get(CommunityPackagesService).hasMissingPackages;
if (this.communityPackagesService) {
this.settings.missingPackages = this.communityPackagesService.hasMissingPackages;
}

this.settings.mfa.enabled = config.get('mfa.enabled');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ export class WorkflowHistoryService {
return hist;
}

async saveVersion(user: User, workflow: WorkflowEntity) {
async saveVersion(user: User, workflow: WorkflowEntity, workflowId: string) {
if (isWorkflowHistoryEnabled()) {
await this.workflowHistoryRepository.insert({
authors: user.firstName + ' ' + user.lastName,
connections: workflow.connections,
nodes: workflow.nodes,
versionId: workflow.versionId,
workflowId: workflow.id,
workflowId,
});
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/workflows/workflows.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { RoleService } from '@/services/role.service';
import * as utils from '@/utils';
import { listQueryMiddleware } from '@/middlewares';
import { TagService } from '@/services/tag.service';
import { isWorkflowHistoryLicensed } from './workflowHistory/workflowHistoryHelper.ee';
import { WorkflowHistoryService } from './workflowHistory/workflowHistory.service.ee';

export const workflowsController = express.Router();

Expand Down Expand Up @@ -99,6 +101,14 @@ workflowsController.post(
throw new ResponseHelper.InternalServerError('Failed to save workflow');
}

if (isWorkflowHistoryLicensed()) {
await Container.get(WorkflowHistoryService).saveVersion(
req.user,
savedWorkflow,
savedWorkflow.id,
);
}

if (tagIds && !config.getEnv('workflowTagsDisabled') && savedWorkflow.tags) {
savedWorkflow.tags = Container.get(TagService).sortByRequestOrder(savedWorkflow.tags, {
requestOrder: tagIds,
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/workflows/workflows.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ export class WorkflowsService {
);
}

if (isWorkflowHistoryLicensed()) {
await Container.get(WorkflowHistoryService).saveVersion(user, shared.workflow);
if (isWorkflowHistoryLicensed() && workflow.versionId !== shared.workflow.versionId) {
await Container.get(WorkflowHistoryService).saveVersion(user, workflow, workflowId);
}

const relations = config.getEnv('workflowTagsDisabled') ? [] : ['tags'];
Expand Down
Loading

0 comments on commit 12fa870

Please sign in to comment.