Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3de18ab46f | ||
![]() |
54f509ee38 | ||
![]() |
978ceaadb6 | ||
![]() |
770b07179f | ||
![]() |
6d15167ad9 | ||
![]() |
39cba6bc74 | ||
![]() |
9558e66abf | ||
![]() |
ff7908955e | ||
![]() |
26b095b835 |
@@ -87,14 +87,14 @@ describe('GET /api/v1/apps/:appKey/connections', () => {
|
|||||||
|
|
||||||
it('should return not found response for invalid connection UUID', async () => {
|
it('should return not found response for invalid connection UUID', async () => {
|
||||||
await createPermission({
|
await createPermission({
|
||||||
action: 'update',
|
action: 'read',
|
||||||
subject: 'Connection',
|
subject: 'Connection',
|
||||||
roleId: currentUserRole.id,
|
roleId: currentUserRole.id,
|
||||||
conditions: ['isCreator'],
|
conditions: ['isCreator'],
|
||||||
});
|
});
|
||||||
|
|
||||||
await request(app)
|
await request(app)
|
||||||
.get('/api/v1/connections/invalid-connection-id/connections')
|
.get('/api/v1/apps/invalid-connection-id/connections')
|
||||||
.set('Authorization', token)
|
.set('Authorization', token)
|
||||||
.expect(404);
|
.expect(404);
|
||||||
});
|
});
|
||||||
|
@@ -193,7 +193,7 @@ describe('POST /api/v1/steps/:stepId/dynamic-data', () => {
|
|||||||
const notExistingStepUUID = Crypto.randomUUID();
|
const notExistingStepUUID = Crypto.randomUUID();
|
||||||
|
|
||||||
await request(app)
|
await request(app)
|
||||||
.get(`/api/v1/steps/${notExistingStepUUID}/dynamic-data`)
|
.post(`/api/v1/steps/${notExistingStepUUID}/dynamic-data`)
|
||||||
.set('Authorization', token)
|
.set('Authorization', token)
|
||||||
.expect(404);
|
.expect(404);
|
||||||
});
|
});
|
||||||
@@ -216,7 +216,7 @@ describe('POST /api/v1/steps/:stepId/dynamic-data', () => {
|
|||||||
const step = await createStep({ appKey: null });
|
const step = await createStep({ appKey: null });
|
||||||
|
|
||||||
await request(app)
|
await request(app)
|
||||||
.get(`/api/v1/steps/${step.id}/dynamic-data`)
|
.post(`/api/v1/steps/${step.id}/dynamic-data`)
|
||||||
.set('Authorization', token)
|
.set('Authorization', token)
|
||||||
.expect(404);
|
.expect(404);
|
||||||
});
|
});
|
||||||
|
@@ -118,7 +118,7 @@ describe('POST /api/v1/steps/:stepId/dynamic-fields', () => {
|
|||||||
const notExistingStepUUID = Crypto.randomUUID();
|
const notExistingStepUUID = Crypto.randomUUID();
|
||||||
|
|
||||||
await request(app)
|
await request(app)
|
||||||
.get(`/api/v1/steps/${notExistingStepUUID}/dynamic-fields`)
|
.post(`/api/v1/steps/${notExistingStepUUID}/dynamic-fields`)
|
||||||
.set('Authorization', token)
|
.set('Authorization', token)
|
||||||
.expect(404);
|
.expect(404);
|
||||||
});
|
});
|
||||||
@@ -138,10 +138,11 @@ describe('POST /api/v1/steps/:stepId/dynamic-fields', () => {
|
|||||||
conditions: [],
|
conditions: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
const step = await createStep({ appKey: null });
|
const step = await createStep();
|
||||||
|
await step.$query().patch({ appKey: null });
|
||||||
|
|
||||||
await request(app)
|
await request(app)
|
||||||
.get(`/api/v1/steps/${step.id}/dynamic-fields`)
|
.post(`/api/v1/steps/${step.id}/dynamic-fields`)
|
||||||
.set('Authorization', token)
|
.set('Authorization', token)
|
||||||
.expect(404);
|
.expect(404);
|
||||||
});
|
});
|
||||||
|
@@ -996,21 +996,9 @@ describe('User model', () => {
|
|||||||
|
|
||||||
const user = await createUser();
|
const user = await createUser();
|
||||||
|
|
||||||
const presentDate = DateTime.fromObject(
|
|
||||||
{ year: 2024, month: 11, day: 17, hour: 11, minute: 30 },
|
|
||||||
{ zone: 'UTC+0' }
|
|
||||||
);
|
|
||||||
|
|
||||||
vi.setSystemTime(presentDate);
|
|
||||||
|
|
||||||
await user.startTrialPeriod();
|
await user.startTrialPeriod();
|
||||||
|
|
||||||
const futureDate = DateTime.fromObject(
|
vi.setSystemTime(DateTime.now().plus({ month: 1 }));
|
||||||
{ year: 2025, month: 1, day: 1 },
|
|
||||||
{ zone: 'UTC+0' }
|
|
||||||
);
|
|
||||||
|
|
||||||
vi.setSystemTime(futureDate);
|
|
||||||
|
|
||||||
const refetchedUser = await user.$query();
|
const refetchedUser = await user.$query();
|
||||||
|
|
||||||
@@ -1118,7 +1106,9 @@ describe('User model', () => {
|
|||||||
|
|
||||||
const user = await createUser();
|
const user = await createUser();
|
||||||
|
|
||||||
expect(() => user.getPlanAndUsage()).rejects.toThrow('NotFoundError');
|
await expect(() => user.getPlanAndUsage()).rejects.toThrow(
|
||||||
|
'NotFoundError'
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1189,7 +1179,7 @@ describe('User model', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should throw not found error when user role does not exist', async () => {
|
it('should throw not found error when user role does not exist', async () => {
|
||||||
expect(() =>
|
await expect(() =>
|
||||||
User.registerUser({
|
User.registerUser({
|
||||||
fullName: 'Sample user',
|
fullName: 'Sample user',
|
||||||
email: 'user@automatisch.io',
|
email: 'user@automatisch.io',
|
||||||
@@ -1257,6 +1247,8 @@ describe('User model', () => {
|
|||||||
|
|
||||||
describe('createUsageData', () => {
|
describe('createUsageData', () => {
|
||||||
it('should create usage data if Automatisch is a cloud installation', async () => {
|
it('should create usage data if Automatisch is a cloud installation', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
|
||||||
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(true);
|
vi.spyOn(appConfig, 'isCloud', 'get').mockReturnValue(true);
|
||||||
|
|
||||||
const user = await createUser({
|
const user = await createUser({
|
||||||
@@ -1264,10 +1256,14 @@ describe('User model', () => {
|
|||||||
email: 'user@automatisch.io',
|
email: 'user@automatisch.io',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
vi.setSystemTime(DateTime.now().plus({ month: 1 }));
|
||||||
|
|
||||||
const usageData = await user.createUsageData();
|
const usageData = await user.createUsageData();
|
||||||
const currentUsageData = await user.$relatedQuery('currentUsageData');
|
const currentUsageData = await user.$relatedQuery('currentUsageData');
|
||||||
|
|
||||||
expect(usageData).toStrictEqual(currentUsageData);
|
expect(usageData).toStrictEqual(currentUsageData);
|
||||||
|
|
||||||
|
vi.useRealTimers();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not create usage data if Automatisch is not a cloud installation', async () => {
|
it('should not create usage data if Automatisch is not a cloud installation', async () => {
|
||||||
|
@@ -11,10 +11,6 @@ const redisConnection = {
|
|||||||
|
|
||||||
const actionQueue = new Queue('action', redisConnection);
|
const actionQueue = new Queue('action', redisConnection);
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
|
||||||
await actionQueue.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
actionQueue.on('error', (error) => {
|
actionQueue.on('error', (error) => {
|
||||||
if (error.code === CONNECTION_REFUSED) {
|
if (error.code === CONNECTION_REFUSED) {
|
||||||
logger.error(
|
logger.error(
|
||||||
|
@@ -11,10 +11,6 @@ const redisConnection = {
|
|||||||
|
|
||||||
const deleteUserQueue = new Queue('delete-user', redisConnection);
|
const deleteUserQueue = new Queue('delete-user', redisConnection);
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
|
||||||
await deleteUserQueue.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
deleteUserQueue.on('error', (error) => {
|
deleteUserQueue.on('error', (error) => {
|
||||||
if (error.code === CONNECTION_REFUSED) {
|
if (error.code === CONNECTION_REFUSED) {
|
||||||
logger.error(
|
logger.error(
|
||||||
|
@@ -11,10 +11,6 @@ const redisConnection = {
|
|||||||
|
|
||||||
const emailQueue = new Queue('email', redisConnection);
|
const emailQueue = new Queue('email', redisConnection);
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
|
||||||
await emailQueue.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
emailQueue.on('error', (error) => {
|
emailQueue.on('error', (error) => {
|
||||||
if (error.code === CONNECTION_REFUSED) {
|
if (error.code === CONNECTION_REFUSED) {
|
||||||
logger.error(
|
logger.error(
|
||||||
|
@@ -11,10 +11,6 @@ const redisConnection = {
|
|||||||
|
|
||||||
const flowQueue = new Queue('flow', redisConnection);
|
const flowQueue = new Queue('flow', redisConnection);
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
|
||||||
await flowQueue.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
flowQueue.on('error', (error) => {
|
flowQueue.on('error', (error) => {
|
||||||
if (error.code === CONNECTION_REFUSED) {
|
if (error.code === CONNECTION_REFUSED) {
|
||||||
logger.error(
|
logger.error(
|
||||||
|
21
packages/backend/src/queues/index.js
Normal file
21
packages/backend/src/queues/index.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import appConfig from '../config/app.js';
|
||||||
|
import actionQueue from './action.js';
|
||||||
|
import emailQueue from './email.js';
|
||||||
|
import flowQueue from './flow.js';
|
||||||
|
import triggerQueue from './trigger.js';
|
||||||
|
import deleteUserQueue from './delete-user.ee.js';
|
||||||
|
import removeCancelledSubscriptionsQueue from './remove-cancelled-subscriptions.ee.js';
|
||||||
|
|
||||||
|
const queues = [
|
||||||
|
actionQueue,
|
||||||
|
emailQueue,
|
||||||
|
flowQueue,
|
||||||
|
triggerQueue,
|
||||||
|
deleteUserQueue,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (appConfig.isCloud) {
|
||||||
|
queues.push(removeCancelledSubscriptionsQueue);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default queues;
|
@@ -14,10 +14,6 @@ const removeCancelledSubscriptionsQueue = new Queue(
|
|||||||
redisConnection
|
redisConnection
|
||||||
);
|
);
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
|
||||||
await removeCancelledSubscriptionsQueue.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
removeCancelledSubscriptionsQueue.on('error', (error) => {
|
removeCancelledSubscriptionsQueue.on('error', (error) => {
|
||||||
if (error.code === CONNECTION_REFUSED) {
|
if (error.code === CONNECTION_REFUSED) {
|
||||||
logger.error(
|
logger.error(
|
||||||
|
@@ -11,10 +11,6 @@ const redisConnection = {
|
|||||||
|
|
||||||
const triggerQueue = new Queue('trigger', redisConnection);
|
const triggerQueue = new Queue('trigger', redisConnection);
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
|
||||||
await triggerQueue.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
triggerQueue.on('error', (error) => {
|
triggerQueue.on('error', (error) => {
|
||||||
if (error.code === CONNECTION_REFUSED) {
|
if (error.code === CONNECTION_REFUSED) {
|
||||||
logger.error(
|
logger.error(
|
||||||
|
@@ -1,20 +1,22 @@
|
|||||||
import * as Sentry from './helpers/sentry.ee.js';
|
import * as Sentry from './helpers/sentry.ee.js';
|
||||||
import appConfig from './config/app.js';
|
import process from 'node:process';
|
||||||
|
|
||||||
Sentry.init();
|
Sentry.init();
|
||||||
|
|
||||||
import './config/orm.js';
|
import './config/orm.js';
|
||||||
import './helpers/check-worker-readiness.js';
|
import './helpers/check-worker-readiness.js';
|
||||||
import './workers/flow.js';
|
import queues from './queues/index.js';
|
||||||
import './workers/trigger.js';
|
import workers from './workers/index.js';
|
||||||
import './workers/action.js';
|
|
||||||
import './workers/email.js';
|
|
||||||
import './workers/delete-user.ee.js';
|
|
||||||
|
|
||||||
if (appConfig.isCloud) {
|
process.on('SIGTERM', async () => {
|
||||||
import('./workers/remove-cancelled-subscriptions.ee.js');
|
for (const queue of queues) {
|
||||||
import('./queues/remove-cancelled-subscriptions.ee.js');
|
await queue.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const worker of workers) {
|
||||||
|
await worker.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
import telemetry from './helpers/telemetry/index.js';
|
import telemetry from './helpers/telemetry/index.js';
|
||||||
|
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
import { Worker } from 'bullmq';
|
import { Worker } from 'bullmq';
|
||||||
import process from 'node:process';
|
|
||||||
|
|
||||||
import * as Sentry from '../helpers/sentry.ee.js';
|
import * as Sentry from '../helpers/sentry.ee.js';
|
||||||
import redisConfig from '../config/redis.js';
|
import redisConfig from '../config/redis.js';
|
||||||
@@ -15,7 +14,7 @@ import delayAsMilliseconds from '../helpers/delay-as-milliseconds.js';
|
|||||||
|
|
||||||
const DEFAULT_DELAY_DURATION = 0;
|
const DEFAULT_DELAY_DURATION = 0;
|
||||||
|
|
||||||
export const worker = new Worker(
|
const actionWorker = new Worker(
|
||||||
'action',
|
'action',
|
||||||
async (job) => {
|
async (job) => {
|
||||||
const { stepId, flowId, executionId, computedParameters, executionStep } =
|
const { stepId, flowId, executionId, computedParameters, executionStep } =
|
||||||
@@ -55,11 +54,11 @@ export const worker = new Worker(
|
|||||||
{ connection: redisConfig }
|
{ connection: redisConfig }
|
||||||
);
|
);
|
||||||
|
|
||||||
worker.on('completed', (job) => {
|
actionWorker.on('completed', (job) => {
|
||||||
logger.info(`JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has started!`);
|
logger.info(`JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has started!`);
|
||||||
});
|
});
|
||||||
|
|
||||||
worker.on('failed', (job, err) => {
|
actionWorker.on('failed', (job, err) => {
|
||||||
const errorMessage = `
|
const errorMessage = `
|
||||||
JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has failed to start with ${err.message}
|
JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has failed to start with ${err.message}
|
||||||
\n ${err.stack}
|
\n ${err.stack}
|
||||||
@@ -74,6 +73,4 @@ worker.on('failed', (job, err) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
export default actionWorker;
|
||||||
await worker.close();
|
|
||||||
});
|
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
import { Worker } from 'bullmq';
|
import { Worker } from 'bullmq';
|
||||||
import process from 'node:process';
|
|
||||||
|
|
||||||
import * as Sentry from '../helpers/sentry.ee.js';
|
import * as Sentry from '../helpers/sentry.ee.js';
|
||||||
import redisConfig from '../config/redis.js';
|
import redisConfig from '../config/redis.js';
|
||||||
@@ -8,7 +7,7 @@ import appConfig from '../config/app.js';
|
|||||||
import User from '../models/user.js';
|
import User from '../models/user.js';
|
||||||
import ExecutionStep from '../models/execution-step.js';
|
import ExecutionStep from '../models/execution-step.js';
|
||||||
|
|
||||||
export const worker = new Worker(
|
const deleteUserWorker = new Worker(
|
||||||
'delete-user',
|
'delete-user',
|
||||||
async (job) => {
|
async (job) => {
|
||||||
const { id } = job.data;
|
const { id } = job.data;
|
||||||
@@ -46,13 +45,13 @@ export const worker = new Worker(
|
|||||||
{ connection: redisConfig }
|
{ connection: redisConfig }
|
||||||
);
|
);
|
||||||
|
|
||||||
worker.on('completed', (job) => {
|
deleteUserWorker.on('completed', (job) => {
|
||||||
logger.info(
|
logger.info(
|
||||||
`JOB ID: ${job.id} - The user with the ID of '${job.data.id}' has been deleted!`
|
`JOB ID: ${job.id} - The user with the ID of '${job.data.id}' has been deleted!`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
worker.on('failed', (job, err) => {
|
deleteUserWorker.on('failed', (job, err) => {
|
||||||
const errorMessage = `
|
const errorMessage = `
|
||||||
JOB ID: ${job.id} - The user with the ID of '${job.data.id}' has failed to be deleted! ${err.message}
|
JOB ID: ${job.id} - The user with the ID of '${job.data.id}' has failed to be deleted! ${err.message}
|
||||||
\n ${err.stack}
|
\n ${err.stack}
|
||||||
@@ -67,6 +66,4 @@ worker.on('failed', (job, err) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
export default deleteUserWorker;
|
||||||
await worker.close();
|
|
||||||
});
|
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
import { Worker } from 'bullmq';
|
import { Worker } from 'bullmq';
|
||||||
import process from 'node:process';
|
|
||||||
|
|
||||||
import * as Sentry from '../helpers/sentry.ee.js';
|
import * as Sentry from '../helpers/sentry.ee.js';
|
||||||
import redisConfig from '../config/redis.js';
|
import redisConfig from '../config/redis.js';
|
||||||
@@ -16,7 +15,7 @@ const isAutomatischEmail = (email) => {
|
|||||||
return email.endsWith('@automatisch.io');
|
return email.endsWith('@automatisch.io');
|
||||||
};
|
};
|
||||||
|
|
||||||
export const worker = new Worker(
|
const emailWorker = new Worker(
|
||||||
'email',
|
'email',
|
||||||
async (job) => {
|
async (job) => {
|
||||||
const { email, subject, template, params } = job.data;
|
const { email, subject, template, params } = job.data;
|
||||||
@@ -39,13 +38,13 @@ export const worker = new Worker(
|
|||||||
{ connection: redisConfig }
|
{ connection: redisConfig }
|
||||||
);
|
);
|
||||||
|
|
||||||
worker.on('completed', (job) => {
|
emailWorker.on('completed', (job) => {
|
||||||
logger.info(
|
logger.info(
|
||||||
`JOB ID: ${job.id} - ${job.data.subject} email sent to ${job.data.email}!`
|
`JOB ID: ${job.id} - ${job.data.subject} email sent to ${job.data.email}!`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
worker.on('failed', (job, err) => {
|
emailWorker.on('failed', (job, err) => {
|
||||||
const errorMessage = `
|
const errorMessage = `
|
||||||
JOB ID: ${job.id} - ${job.data.subject} email to ${job.data.email} has failed to send with ${err.message}
|
JOB ID: ${job.id} - ${job.data.subject} email to ${job.data.email} has failed to send with ${err.message}
|
||||||
\n ${err.stack}
|
\n ${err.stack}
|
||||||
@@ -60,6 +59,4 @@ worker.on('failed', (job, err) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
export default emailWorker;
|
||||||
await worker.close();
|
|
||||||
});
|
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
import { Worker } from 'bullmq';
|
import { Worker } from 'bullmq';
|
||||||
import process from 'node:process';
|
|
||||||
|
|
||||||
import * as Sentry from '../helpers/sentry.ee.js';
|
import * as Sentry from '../helpers/sentry.ee.js';
|
||||||
import redisConfig from '../config/redis.js';
|
import redisConfig from '../config/redis.js';
|
||||||
@@ -13,7 +12,7 @@ import {
|
|||||||
REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
||||||
} from '../helpers/remove-job-configuration.js';
|
} from '../helpers/remove-job-configuration.js';
|
||||||
|
|
||||||
export const worker = new Worker(
|
const flowWorker = new Worker(
|
||||||
'flow',
|
'flow',
|
||||||
async (job) => {
|
async (job) => {
|
||||||
const { flowId } = job.data;
|
const { flowId } = job.data;
|
||||||
@@ -64,11 +63,11 @@ export const worker = new Worker(
|
|||||||
{ connection: redisConfig }
|
{ connection: redisConfig }
|
||||||
);
|
);
|
||||||
|
|
||||||
worker.on('completed', (job) => {
|
flowWorker.on('completed', (job) => {
|
||||||
logger.info(`JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has started!`);
|
logger.info(`JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has started!`);
|
||||||
});
|
});
|
||||||
|
|
||||||
worker.on('failed', async (job, err) => {
|
flowWorker.on('failed', async (job, err) => {
|
||||||
const errorMessage = `
|
const errorMessage = `
|
||||||
JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has failed to start with ${err.message}
|
JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has failed to start with ${err.message}
|
||||||
\n ${err.stack}
|
\n ${err.stack}
|
||||||
@@ -95,6 +94,4 @@ worker.on('failed', async (job, err) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
export default flowWorker;
|
||||||
await worker.close();
|
|
||||||
});
|
|
||||||
|
21
packages/backend/src/workers/index.js
Normal file
21
packages/backend/src/workers/index.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import appConfig from '../config/app.js';
|
||||||
|
import actionWorker from './action.js';
|
||||||
|
import emailWorker from './email.js';
|
||||||
|
import flowWorker from './flow.js';
|
||||||
|
import triggerWorker from './trigger.js';
|
||||||
|
import deleteUserWorker from './delete-user.ee.js';
|
||||||
|
import removeCancelledSubscriptionsWorker from './remove-cancelled-subscriptions.ee.js';
|
||||||
|
|
||||||
|
const workers = [
|
||||||
|
actionWorker,
|
||||||
|
emailWorker,
|
||||||
|
flowWorker,
|
||||||
|
triggerWorker,
|
||||||
|
deleteUserWorker,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (appConfig.isCloud) {
|
||||||
|
workers.push(removeCancelledSubscriptionsWorker);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default workers;
|
@@ -1,12 +1,11 @@
|
|||||||
import { Worker } from 'bullmq';
|
import { Worker } from 'bullmq';
|
||||||
import process from 'node:process';
|
|
||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
import * as Sentry from '../helpers/sentry.ee.js';
|
import * as Sentry from '../helpers/sentry.ee.js';
|
||||||
import redisConfig from '../config/redis.js';
|
import redisConfig from '../config/redis.js';
|
||||||
import logger from '../helpers/logger.js';
|
import logger from '../helpers/logger.js';
|
||||||
import Subscription from '../models/subscription.ee.js';
|
import Subscription from '../models/subscription.ee.js';
|
||||||
|
|
||||||
export const worker = new Worker(
|
const removeCancelledSubscriptionsWorker = new Worker(
|
||||||
'remove-cancelled-subscriptions',
|
'remove-cancelled-subscriptions',
|
||||||
async () => {
|
async () => {
|
||||||
await Subscription.query()
|
await Subscription.query()
|
||||||
@@ -23,13 +22,13 @@ export const worker = new Worker(
|
|||||||
{ connection: redisConfig }
|
{ connection: redisConfig }
|
||||||
);
|
);
|
||||||
|
|
||||||
worker.on('completed', (job) => {
|
removeCancelledSubscriptionsWorker.on('completed', (job) => {
|
||||||
logger.info(
|
logger.info(
|
||||||
`JOB ID: ${job.id} - The cancelled subscriptions have been removed!`
|
`JOB ID: ${job.id} - The cancelled subscriptions have been removed!`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
worker.on('failed', (job, err) => {
|
removeCancelledSubscriptionsWorker.on('failed', (job, err) => {
|
||||||
const errorMessage = `
|
const errorMessage = `
|
||||||
JOB ID: ${job.id} - ERROR: The cancelled subscriptions can not be removed! ${err.message}
|
JOB ID: ${job.id} - ERROR: The cancelled subscriptions can not be removed! ${err.message}
|
||||||
\n ${err.stack}
|
\n ${err.stack}
|
||||||
@@ -42,6 +41,4 @@ worker.on('failed', (job, err) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
export default removeCancelledSubscriptionsWorker;
|
||||||
await worker.close();
|
|
||||||
});
|
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
import { Worker } from 'bullmq';
|
import { Worker } from 'bullmq';
|
||||||
import process from 'node:process';
|
|
||||||
|
|
||||||
import * as Sentry from '../helpers/sentry.ee.js';
|
import * as Sentry from '../helpers/sentry.ee.js';
|
||||||
import redisConfig from '../config/redis.js';
|
import redisConfig from '../config/redis.js';
|
||||||
@@ -12,7 +11,7 @@ import {
|
|||||||
REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
||||||
} from '../helpers/remove-job-configuration.js';
|
} from '../helpers/remove-job-configuration.js';
|
||||||
|
|
||||||
export const worker = new Worker(
|
const triggerWorker = new Worker(
|
||||||
'trigger',
|
'trigger',
|
||||||
async (job) => {
|
async (job) => {
|
||||||
const { flowId, executionId, stepId, executionStep } = await processTrigger(
|
const { flowId, executionId, stepId, executionStep } = await processTrigger(
|
||||||
@@ -41,11 +40,11 @@ export const worker = new Worker(
|
|||||||
{ connection: redisConfig }
|
{ connection: redisConfig }
|
||||||
);
|
);
|
||||||
|
|
||||||
worker.on('completed', (job) => {
|
triggerWorker.on('completed', (job) => {
|
||||||
logger.info(`JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has started!`);
|
logger.info(`JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has started!`);
|
||||||
});
|
});
|
||||||
|
|
||||||
worker.on('failed', (job, err) => {
|
triggerWorker.on('failed', (job, err) => {
|
||||||
const errorMessage = `
|
const errorMessage = `
|
||||||
JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has failed to start with ${err.message}
|
JOB ID: ${job.id} - FLOW ID: ${job.data.flowId} has failed to start with ${err.message}
|
||||||
\n ${err.stack}
|
\n ${err.stack}
|
||||||
@@ -60,6 +59,4 @@ worker.on('failed', (job, err) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
export default triggerWorker;
|
||||||
await worker.close();
|
|
||||||
});
|
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { BasePage } from "./base-page";
|
import { BasePage } from './base-page';
|
||||||
const { faker } = require('@faker-js/faker');
|
const { faker } = require('@faker-js/faker');
|
||||||
const { expect } = require('@playwright/test');
|
const { expect } = require('@playwright/test');
|
||||||
|
|
||||||
@@ -6,16 +6,18 @@ export class AdminSetupPage extends BasePage {
|
|||||||
path = '/installation';
|
path = '/installation';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {import('@playwright/test').Page} page
|
* @param {import('@playwright/test').Page} page
|
||||||
*/
|
*/
|
||||||
constructor(page) {
|
constructor(page) {
|
||||||
super(page);
|
super(page);
|
||||||
|
|
||||||
this.fullNameTextField = this.page.getByTestId('fullName-text-field');
|
this.fullNameTextField = this.page.getByTestId('fullName-text-field');
|
||||||
this.emailTextField = this.page.getByTestId('email-text-field');
|
this.emailTextField = this.page.getByTestId('email-text-field');
|
||||||
this.passwordTextField = this.page.getByTestId('password-text-field');
|
this.passwordTextField = this.page.getByTestId('password-text-field');
|
||||||
this.repeatPasswordTextField = this.page.getByTestId('repeat-password-text-field');
|
this.repeatPasswordTextField = this.page.getByTestId(
|
||||||
this.createAdminButton = this.page.getByTestId('signUp-button');
|
'repeat-password-text-field'
|
||||||
|
);
|
||||||
|
this.createAdminButton = this.page.getByTestId('installation-button');
|
||||||
this.invalidFields = this.page.locator('p.Mui-error');
|
this.invalidFields = this.page.locator('p.Mui-error');
|
||||||
this.successAlert = this.page.getByTestId('success-alert');
|
this.successAlert = this.page.getByTestId('success-alert');
|
||||||
}
|
}
|
||||||
@@ -46,7 +48,7 @@ export class AdminSetupPage extends BasePage {
|
|||||||
await this.repeatPasswordTextField.fill(testUser.wronglyRepeatedPassword);
|
await this.repeatPasswordTextField.fill(testUser.wronglyRepeatedPassword);
|
||||||
}
|
}
|
||||||
|
|
||||||
async submitAdminForm() {
|
async submitAdminForm() {
|
||||||
await this.createAdminButton.click();
|
await this.createAdminButton.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +61,10 @@ export class AdminSetupPage extends BasePage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async expectSuccessMessageToContainLoginLink() {
|
async expectSuccessMessageToContainLoginLink() {
|
||||||
await expect(await this.successAlert.locator('a')).toHaveAttribute('href', '/login');
|
await expect(await this.successAlert.locator('a')).toHaveAttribute(
|
||||||
|
'href',
|
||||||
|
'/login'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
generateUser() {
|
generateUser() {
|
||||||
@@ -69,7 +74,7 @@ export class AdminSetupPage extends BasePage {
|
|||||||
fullName: faker.person.fullName(),
|
fullName: faker.person.fullName(),
|
||||||
email: faker.internet.email(),
|
email: faker.internet.email(),
|
||||||
password: faker.internet.password(),
|
password: faker.internet.password(),
|
||||||
wronglyRepeatedPassword: faker.internet.password()
|
wronglyRepeatedPassword: faker.internet.password(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
@@ -46,7 +46,12 @@ function Form(props) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<FormProvider {...methods}>
|
<FormProvider {...methods}>
|
||||||
<form onSubmit={methods.handleSubmit(onSubmit)} {...formProps}>
|
<form
|
||||||
|
onSubmit={methods.handleSubmit((data, event) =>
|
||||||
|
onSubmit?.(data, event, methods.setError),
|
||||||
|
)}
|
||||||
|
{...formProps}
|
||||||
|
>
|
||||||
{render ? render(methods) : children}
|
{render ? render(methods) : children}
|
||||||
</form>
|
</form>
|
||||||
</FormProvider>
|
</FormProvider>
|
||||||
|
@@ -2,35 +2,55 @@ import * as React from 'react';
|
|||||||
import { Link as RouterLink } from 'react-router-dom';
|
import { Link as RouterLink } from 'react-router-dom';
|
||||||
import Paper from '@mui/material/Paper';
|
import Paper from '@mui/material/Paper';
|
||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
import { Alert } from '@mui/material';
|
import Alert from '@mui/material/Alert';
|
||||||
import LoadingButton from '@mui/lab/LoadingButton';
|
import LoadingButton from '@mui/lab/LoadingButton';
|
||||||
import * as yup from 'yup';
|
import * as yup from 'yup';
|
||||||
import { yupResolver } from '@hookform/resolvers/yup';
|
import { yupResolver } from '@hookform/resolvers/yup';
|
||||||
import { enqueueSnackbar } from 'notistack';
|
|
||||||
import { useQueryClient } from '@tanstack/react-query';
|
import { useQueryClient } from '@tanstack/react-query';
|
||||||
import Link from '@mui/material/Link';
|
import Link from '@mui/material/Link';
|
||||||
|
|
||||||
|
import { getGeneralErrorMessage } from 'helpers/errors';
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
import useInstallation from 'hooks/useInstallation';
|
import useInstallation from 'hooks/useInstallation';
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import Form from 'components/Form';
|
import Form from 'components/Form';
|
||||||
import TextField from 'components/TextField';
|
import TextField from 'components/TextField';
|
||||||
|
|
||||||
const validationSchema = yup.object().shape({
|
const getValidationSchema = (formatMessage) => {
|
||||||
fullName: yup.string().trim().required('installationForm.mandatoryInput'),
|
const getMandatoryInputMessage = (inputNameId) =>
|
||||||
email: yup
|
formatMessage('installationForm.mandatoryInput', {
|
||||||
.string()
|
inputName: formatMessage(inputNameId),
|
||||||
.trim()
|
});
|
||||||
.email('installationForm.validateEmail')
|
|
||||||
.required('installationForm.mandatoryInput'),
|
|
||||||
password: yup.string().required('installationForm.mandatoryInput'),
|
|
||||||
confirmPassword: yup
|
|
||||||
.string()
|
|
||||||
.required('installationForm.mandatoryInput')
|
|
||||||
.oneOf([yup.ref('password')], 'installationForm.passwordsMustMatch'),
|
|
||||||
});
|
|
||||||
|
|
||||||
const initialValues = {
|
return yup.object().shape({
|
||||||
|
fullName: yup
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.required(
|
||||||
|
getMandatoryInputMessage('installationForm.fullNameFieldLabel'),
|
||||||
|
),
|
||||||
|
email: yup
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.required(getMandatoryInputMessage('installationForm.emailFieldLabel'))
|
||||||
|
.email(formatMessage('installationForm.validateEmail')),
|
||||||
|
password: yup
|
||||||
|
.string()
|
||||||
|
.required(getMandatoryInputMessage('installationForm.passwordFieldLabel'))
|
||||||
|
.min(6, formatMessage('installationForm.passwordMinLength')),
|
||||||
|
confirmPassword: yup
|
||||||
|
.string()
|
||||||
|
.required(
|
||||||
|
getMandatoryInputMessage('installationForm.confirmPasswordFieldLabel'),
|
||||||
|
)
|
||||||
|
.oneOf(
|
||||||
|
[yup.ref('password')],
|
||||||
|
formatMessage('installationForm.passwordsMustMatch'),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultValues = {
|
||||||
fullName: '',
|
fullName: '',
|
||||||
email: '',
|
email: '',
|
||||||
password: '',
|
password: '',
|
||||||
@@ -39,7 +59,7 @@ const initialValues = {
|
|||||||
|
|
||||||
function InstallationForm() {
|
function InstallationForm() {
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const install = useInstallation();
|
const { mutateAsync: install, isSuccess, isPending } = useInstallation();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const handleOnRedirect = () => {
|
const handleOnRedirect = () => {
|
||||||
@@ -48,21 +68,38 @@ function InstallationForm() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = async (values) => {
|
const handleSubmit = async ({ fullName, email, password }, e, setError) => {
|
||||||
const { fullName, email, password } = values;
|
|
||||||
try {
|
try {
|
||||||
await install.mutateAsync({
|
await install({
|
||||||
fullName,
|
fullName,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
enqueueSnackbar(
|
const errors = error?.response?.data?.errors;
|
||||||
error?.message || formatMessage('installationForm.error'),
|
if (errors) {
|
||||||
{
|
const fieldNames = Object.keys(defaultValues);
|
||||||
variant: 'error',
|
Object.entries(errors).forEach(([fieldName, fieldErrors]) => {
|
||||||
},
|
if (fieldNames.includes(fieldName) && Array.isArray(fieldErrors)) {
|
||||||
);
|
setError(fieldName, {
|
||||||
|
type: 'fieldRequestError',
|
||||||
|
message: fieldErrors.join(', '),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const generalError = getGeneralErrorMessage({
|
||||||
|
error,
|
||||||
|
fallbackMessage: formatMessage('installationForm.error'),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (generalError) {
|
||||||
|
setError('root.general', {
|
||||||
|
type: 'requestError',
|
||||||
|
message: generalError,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -82,11 +119,13 @@ function InstallationForm() {
|
|||||||
{formatMessage('installationForm.title')}
|
{formatMessage('installationForm.title')}
|
||||||
</Typography>
|
</Typography>
|
||||||
<Form
|
<Form
|
||||||
defaultValues={initialValues}
|
automaticValidation={false}
|
||||||
|
noValidate
|
||||||
|
defaultValues={defaultValues}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
resolver={yupResolver(validationSchema)}
|
resolver={yupResolver(getValidationSchema(formatMessage))}
|
||||||
mode="onChange"
|
mode="onChange"
|
||||||
render={({ formState: { errors, touchedFields } }) => (
|
render={({ formState: { errors } }) => (
|
||||||
<>
|
<>
|
||||||
<TextField
|
<TextField
|
||||||
label={formatMessage('installationForm.fullNameFieldLabel')}
|
label={formatMessage('installationForm.fullNameFieldLabel')}
|
||||||
@@ -95,19 +134,12 @@ function InstallationForm() {
|
|||||||
margin="dense"
|
margin="dense"
|
||||||
autoComplete="fullName"
|
autoComplete="fullName"
|
||||||
data-test="fullName-text-field"
|
data-test="fullName-text-field"
|
||||||
error={touchedFields.fullName && !!errors?.fullName}
|
error={!!errors?.fullName}
|
||||||
helperText={
|
helperText={errors?.fullName?.message}
|
||||||
touchedFields.fullName && errors?.fullName?.message
|
|
||||||
? formatMessage(errors?.fullName?.message, {
|
|
||||||
inputName: formatMessage(
|
|
||||||
'installationForm.fullNameFieldLabel',
|
|
||||||
),
|
|
||||||
})
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
required
|
required
|
||||||
readOnly={install.isSuccess}
|
readOnly={isSuccess}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
label={formatMessage('installationForm.emailFieldLabel')}
|
label={formatMessage('installationForm.emailFieldLabel')}
|
||||||
name="email"
|
name="email"
|
||||||
@@ -115,19 +147,12 @@ function InstallationForm() {
|
|||||||
margin="dense"
|
margin="dense"
|
||||||
autoComplete="email"
|
autoComplete="email"
|
||||||
data-test="email-text-field"
|
data-test="email-text-field"
|
||||||
error={touchedFields.email && !!errors?.email}
|
error={!!errors?.email}
|
||||||
helperText={
|
helperText={errors?.email?.message}
|
||||||
touchedFields.email && errors?.email?.message
|
|
||||||
? formatMessage(errors?.email?.message, {
|
|
||||||
inputName: formatMessage(
|
|
||||||
'installationForm.emailFieldLabel',
|
|
||||||
),
|
|
||||||
})
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
required
|
required
|
||||||
readOnly={install.isSuccess}
|
readOnly={isSuccess}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
label={formatMessage('installationForm.passwordFieldLabel')}
|
label={formatMessage('installationForm.passwordFieldLabel')}
|
||||||
name="password"
|
name="password"
|
||||||
@@ -135,19 +160,12 @@ function InstallationForm() {
|
|||||||
margin="dense"
|
margin="dense"
|
||||||
type="password"
|
type="password"
|
||||||
data-test="password-text-field"
|
data-test="password-text-field"
|
||||||
error={touchedFields.password && !!errors?.password}
|
error={!!errors?.password}
|
||||||
helperText={
|
helperText={errors?.password?.message}
|
||||||
touchedFields.password && errors?.password?.message
|
|
||||||
? formatMessage(errors?.password?.message, {
|
|
||||||
inputName: formatMessage(
|
|
||||||
'installationForm.passwordFieldLabel',
|
|
||||||
),
|
|
||||||
})
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
required
|
required
|
||||||
readOnly={install.isSuccess}
|
readOnly={isSuccess}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
label={formatMessage(
|
label={formatMessage(
|
||||||
'installationForm.confirmPasswordFieldLabel',
|
'installationForm.confirmPasswordFieldLabel',
|
||||||
@@ -157,52 +175,53 @@ function InstallationForm() {
|
|||||||
margin="dense"
|
margin="dense"
|
||||||
type="password"
|
type="password"
|
||||||
data-test="repeat-password-text-field"
|
data-test="repeat-password-text-field"
|
||||||
error={touchedFields.confirmPassword && !!errors?.confirmPassword}
|
error={!!errors?.confirmPassword}
|
||||||
helperText={
|
helperText={errors?.confirmPassword?.message}
|
||||||
touchedFields.confirmPassword &&
|
|
||||||
errors?.confirmPassword?.message
|
|
||||||
? formatMessage(errors?.confirmPassword?.message, {
|
|
||||||
inputName: formatMessage(
|
|
||||||
'installationForm.confirmPasswordFieldLabel',
|
|
||||||
),
|
|
||||||
})
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
required
|
required
|
||||||
readOnly={install.isSuccess}
|
readOnly={isSuccess}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{errors?.root?.general && (
|
||||||
|
<Alert data-test="error-alert" severity="error" sx={{ mt: 2 }}>
|
||||||
|
{errors.root.general.message}
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{isSuccess && (
|
||||||
|
<Alert
|
||||||
|
data-test="success-alert"
|
||||||
|
severity="success"
|
||||||
|
sx={{ mt: 2 }}
|
||||||
|
>
|
||||||
|
{formatMessage('installationForm.success', {
|
||||||
|
link: (str) => (
|
||||||
|
<Link
|
||||||
|
component={RouterLink}
|
||||||
|
to={URLS.LOGIN}
|
||||||
|
onClick={handleOnRedirect}
|
||||||
|
replace
|
||||||
|
>
|
||||||
|
{str}
|
||||||
|
</Link>
|
||||||
|
),
|
||||||
|
})}
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
<LoadingButton
|
<LoadingButton
|
||||||
type="submit"
|
type="submit"
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
sx={{ boxShadow: 2, mt: 3 }}
|
sx={{ boxShadow: 2, mt: 2 }}
|
||||||
loading={install.isPending}
|
loading={isPending}
|
||||||
disabled={install.isSuccess}
|
disabled={isSuccess}
|
||||||
fullWidth
|
fullWidth
|
||||||
data-test="signUp-button"
|
data-test="installation-button"
|
||||||
>
|
>
|
||||||
{formatMessage('installationForm.submit')}
|
{formatMessage('installationForm.submit')}
|
||||||
</LoadingButton>
|
</LoadingButton>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
{install.isSuccess && (
|
|
||||||
<Alert data-test="success-alert" severity="success" sx={{ mt: 3 }}>
|
|
||||||
{formatMessage('installationForm.success', {
|
|
||||||
link: (str) => (
|
|
||||||
<Link
|
|
||||||
component={RouterLink}
|
|
||||||
to={URLS.LOGIN}
|
|
||||||
onClick={handleOnRedirect}
|
|
||||||
replace
|
|
||||||
>
|
|
||||||
{str}
|
|
||||||
</Link>
|
|
||||||
),
|
|
||||||
})}
|
|
||||||
</Alert>
|
|
||||||
)}
|
|
||||||
</Paper>
|
</Paper>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -3,33 +3,52 @@ import { useNavigate } from 'react-router-dom';
|
|||||||
import Paper from '@mui/material/Paper';
|
import Paper from '@mui/material/Paper';
|
||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
import LoadingButton from '@mui/lab/LoadingButton';
|
import LoadingButton from '@mui/lab/LoadingButton';
|
||||||
|
import Alert from '@mui/material/Alert';
|
||||||
import * as yup from 'yup';
|
import * as yup from 'yup';
|
||||||
import { yupResolver } from '@hookform/resolvers/yup';
|
import { yupResolver } from '@hookform/resolvers/yup';
|
||||||
|
|
||||||
|
import { getGeneralErrorMessage } from 'helpers/errors';
|
||||||
import useAuthentication from 'hooks/useAuthentication';
|
import useAuthentication from 'hooks/useAuthentication';
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import Form from 'components/Form';
|
import Form from 'components/Form';
|
||||||
import TextField from 'components/TextField';
|
import TextField from 'components/TextField';
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
import useCreateAccessToken from 'hooks/useCreateAccessToken';
|
import useCreateAccessToken from 'hooks/useCreateAccessToken';
|
||||||
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
|
||||||
import useRegisterUser from 'hooks/useRegisterUser';
|
import useRegisterUser from 'hooks/useRegisterUser';
|
||||||
|
|
||||||
const validationSchema = yup.object().shape({
|
const getValidationSchema = (formatMessage) => {
|
||||||
fullName: yup.string().trim().required('signupForm.mandatoryInput'),
|
const getMandatoryInputMessage = (inputNameId) =>
|
||||||
email: yup
|
formatMessage('signupForm.mandatoryInput', {
|
||||||
.string()
|
inputName: formatMessage(inputNameId),
|
||||||
.trim()
|
});
|
||||||
.email('signupForm.validateEmail')
|
|
||||||
.required('signupForm.mandatoryInput'),
|
|
||||||
password: yup.string().required('signupForm.mandatoryInput'),
|
|
||||||
confirmPassword: yup
|
|
||||||
.string()
|
|
||||||
.required('signupForm.mandatoryInput')
|
|
||||||
.oneOf([yup.ref('password')], 'signupForm.passwordsMustMatch'),
|
|
||||||
});
|
|
||||||
|
|
||||||
const initialValues = {
|
return yup.object().shape({
|
||||||
|
fullName: yup
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.required(getMandatoryInputMessage('signupForm.fullNameFieldLabel')),
|
||||||
|
email: yup
|
||||||
|
.string()
|
||||||
|
.trim()
|
||||||
|
.required(getMandatoryInputMessage('signupForm.emailFieldLabel'))
|
||||||
|
.email(formatMessage('signupForm.validateEmail')),
|
||||||
|
password: yup
|
||||||
|
.string()
|
||||||
|
.required(getMandatoryInputMessage('signupForm.passwordFieldLabel'))
|
||||||
|
.min(6, formatMessage('signupForm.passwordMinLength')),
|
||||||
|
confirmPassword: yup
|
||||||
|
.string()
|
||||||
|
.required(
|
||||||
|
getMandatoryInputMessage('signupForm.confirmPasswordFieldLabel'),
|
||||||
|
)
|
||||||
|
.oneOf(
|
||||||
|
[yup.ref('password')],
|
||||||
|
formatMessage('signupForm.passwordsMustMatch'),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultValues = {
|
||||||
fullName: '',
|
fullName: '',
|
||||||
email: '',
|
email: '',
|
||||||
password: '',
|
password: '',
|
||||||
@@ -40,7 +59,6 @@ function SignUpForm() {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const authentication = useAuthentication();
|
const authentication = useAuthentication();
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const enqueueSnackbar = useEnqueueSnackbar();
|
|
||||||
const { mutateAsync: registerUser, isPending: isRegisterUserPending } =
|
const { mutateAsync: registerUser, isPending: isRegisterUserPending } =
|
||||||
useRegisterUser();
|
useRegisterUser();
|
||||||
const { mutateAsync: createAccessToken, isPending: loginLoading } =
|
const { mutateAsync: createAccessToken, isPending: loginLoading } =
|
||||||
@@ -52,7 +70,7 @@ function SignUpForm() {
|
|||||||
}
|
}
|
||||||
}, [authentication.isAuthenticated]);
|
}, [authentication.isAuthenticated]);
|
||||||
|
|
||||||
const handleSubmit = async (values) => {
|
const handleSubmit = async (values, e, setError) => {
|
||||||
try {
|
try {
|
||||||
const { fullName, email, password } = values;
|
const { fullName, email, password } = values;
|
||||||
await registerUser({
|
await registerUser({
|
||||||
@@ -67,25 +85,28 @@ function SignUpForm() {
|
|||||||
const { token } = data;
|
const { token } = data;
|
||||||
authentication.updateToken(token);
|
authentication.updateToken(token);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errors = error?.response?.data?.errors
|
const errors = error?.response?.data?.errors;
|
||||||
? Object.values(error.response.data.errors)
|
if (errors) {
|
||||||
: [];
|
const fieldNames = Object.keys(defaultValues);
|
||||||
|
Object.entries(errors).forEach(([fieldName, fieldErrors]) => {
|
||||||
|
if (fieldNames.includes(fieldName) && Array.isArray(fieldErrors)) {
|
||||||
|
setError(fieldName, {
|
||||||
|
type: 'fieldRequestError',
|
||||||
|
message: fieldErrors.join(', '),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (errors.length) {
|
const generalError = getGeneralErrorMessage({
|
||||||
for (const [error] of errors) {
|
error,
|
||||||
enqueueSnackbar(error, {
|
fallbackMessage: formatMessage('signupForm.error'),
|
||||||
variant: 'error',
|
});
|
||||||
SnackbarProps: {
|
|
||||||
'data-test': 'snackbar-sign-up-error',
|
if (generalError) {
|
||||||
},
|
setError('root.general', {
|
||||||
});
|
type: 'requestError',
|
||||||
}
|
message: generalError,
|
||||||
} else {
|
|
||||||
enqueueSnackbar(error?.message || formatMessage('signupForm.error'), {
|
|
||||||
variant: 'error',
|
|
||||||
SnackbarProps: {
|
|
||||||
'data-test': 'snackbar-sign-up-error',
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,11 +129,13 @@ function SignUpForm() {
|
|||||||
</Typography>
|
</Typography>
|
||||||
|
|
||||||
<Form
|
<Form
|
||||||
defaultValues={initialValues}
|
automaticValidation={false}
|
||||||
|
noValidate
|
||||||
|
defaultValues={defaultValues}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
resolver={yupResolver(validationSchema)}
|
resolver={yupResolver(getValidationSchema(formatMessage))}
|
||||||
mode="onChange"
|
mode="onChange"
|
||||||
render={({ formState: { errors, touchedFields } }) => (
|
render={({ formState: { errors } }) => (
|
||||||
<>
|
<>
|
||||||
<TextField
|
<TextField
|
||||||
label={formatMessage('signupForm.fullNameFieldLabel')}
|
label={formatMessage('signupForm.fullNameFieldLabel')}
|
||||||
@@ -121,14 +144,9 @@ function SignUpForm() {
|
|||||||
margin="dense"
|
margin="dense"
|
||||||
autoComplete="fullName"
|
autoComplete="fullName"
|
||||||
data-test="fullName-text-field"
|
data-test="fullName-text-field"
|
||||||
error={touchedFields.fullName && !!errors?.fullName}
|
error={!!errors?.fullName}
|
||||||
helperText={
|
helperText={errors?.fullName?.message}
|
||||||
touchedFields.fullName && errors?.fullName?.message
|
required
|
||||||
? formatMessage(errors?.fullName?.message, {
|
|
||||||
inputName: formatMessage('signupForm.fullNameFieldLabel'),
|
|
||||||
})
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
@@ -138,14 +156,9 @@ function SignUpForm() {
|
|||||||
margin="dense"
|
margin="dense"
|
||||||
autoComplete="email"
|
autoComplete="email"
|
||||||
data-test="email-text-field"
|
data-test="email-text-field"
|
||||||
error={touchedFields.email && !!errors?.email}
|
error={!!errors?.email}
|
||||||
helperText={
|
helperText={errors?.email?.message}
|
||||||
touchedFields.email && errors?.email?.message
|
required
|
||||||
? formatMessage(errors?.email?.message, {
|
|
||||||
inputName: formatMessage('signupForm.emailFieldLabel'),
|
|
||||||
})
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
@@ -154,14 +167,9 @@ function SignUpForm() {
|
|||||||
fullWidth
|
fullWidth
|
||||||
margin="dense"
|
margin="dense"
|
||||||
type="password"
|
type="password"
|
||||||
error={touchedFields.password && !!errors?.password}
|
error={!!errors?.password}
|
||||||
helperText={
|
helperText={errors?.password?.message}
|
||||||
touchedFields.password && errors?.password?.message
|
required
|
||||||
? formatMessage(errors?.password?.message, {
|
|
||||||
inputName: formatMessage('signupForm.passwordFieldLabel'),
|
|
||||||
})
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
@@ -170,19 +178,21 @@ function SignUpForm() {
|
|||||||
fullWidth
|
fullWidth
|
||||||
margin="dense"
|
margin="dense"
|
||||||
type="password"
|
type="password"
|
||||||
error={touchedFields.confirmPassword && !!errors?.confirmPassword}
|
error={!!errors?.confirmPassword}
|
||||||
helperText={
|
helperText={errors?.confirmPassword?.message}
|
||||||
touchedFields.confirmPassword &&
|
required
|
||||||
errors?.confirmPassword?.message
|
|
||||||
? formatMessage(errors?.confirmPassword?.message, {
|
|
||||||
inputName: formatMessage(
|
|
||||||
'signupForm.confirmPasswordFieldLabel',
|
|
||||||
),
|
|
||||||
})
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{errors?.root?.general && (
|
||||||
|
<Alert
|
||||||
|
data-test="alert-sign-up-error"
|
||||||
|
severity="error"
|
||||||
|
sx={{ mt: 2 }}
|
||||||
|
>
|
||||||
|
{errors.root.general.message}
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
|
||||||
<LoadingButton
|
<LoadingButton
|
||||||
type="submit"
|
type="submit"
|
||||||
variant="contained"
|
variant="contained"
|
||||||
|
18
packages/web/src/helpers/errors.js
Normal file
18
packages/web/src/helpers/errors.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
// Helpers to extract errors received from the API
|
||||||
|
|
||||||
|
export const getGeneralErrorMessage = ({ error, fallbackMessage }) => {
|
||||||
|
if (!error) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const errors = error?.response?.data?.errors;
|
||||||
|
const generalError = errors?.general;
|
||||||
|
|
||||||
|
if (generalError && Array.isArray(generalError)) {
|
||||||
|
return generalError.join(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!errors) {
|
||||||
|
return error?.message || fallbackMessage;
|
||||||
|
}
|
||||||
|
};
|
@@ -138,6 +138,7 @@
|
|||||||
"installationForm.submit": "Create admin",
|
"installationForm.submit": "Create admin",
|
||||||
"installationForm.validateEmail": "Email must be valid.",
|
"installationForm.validateEmail": "Email must be valid.",
|
||||||
"installationForm.passwordsMustMatch": "Passwords must match.",
|
"installationForm.passwordsMustMatch": "Passwords must match.",
|
||||||
|
"installationForm.passwordMinLength": "Password must be at least 6 characters long.",
|
||||||
"installationForm.mandatoryInput": "{inputName} is required.",
|
"installationForm.mandatoryInput": "{inputName} is required.",
|
||||||
"installationForm.success": "The admin account has been created, and thus, the installation has been completed. You can now log in <link>here</link>.",
|
"installationForm.success": "The admin account has been created, and thus, the installation has been completed. You can now log in <link>here</link>.",
|
||||||
"installationForm.error": "Something went wrong. Please try again.",
|
"installationForm.error": "Something went wrong. Please try again.",
|
||||||
@@ -149,6 +150,7 @@
|
|||||||
"signupForm.submit": "Sign up",
|
"signupForm.submit": "Sign up",
|
||||||
"signupForm.validateEmail": "Email must be valid.",
|
"signupForm.validateEmail": "Email must be valid.",
|
||||||
"signupForm.passwordsMustMatch": "Passwords must match.",
|
"signupForm.passwordsMustMatch": "Passwords must match.",
|
||||||
|
"signupForm.passwordMinLength": "Password must be at least 6 characters long.",
|
||||||
"signupForm.mandatoryInput": "{inputName} is required.",
|
"signupForm.mandatoryInput": "{inputName} is required.",
|
||||||
"signupForm.error": "Something went wrong. Please try again.",
|
"signupForm.error": "Something went wrong. Please try again.",
|
||||||
"loginForm.title": "Login",
|
"loginForm.title": "Login",
|
||||||
|
Reference in New Issue
Block a user