Skip to content

Commit

Permalink
Merge branch 'main' into forms-960-idp-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
usingtechnology authored Mar 7, 2024
2 parents 7598f28 + 5381e73 commit 9e579a8
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 64 deletions.
2 changes: 2 additions & 0 deletions app/frontend/src/store/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ export const useFormStore = defineStore('form', {
const { data } = await formService.readFormSubscriptionData(formId);
if (data) {
this.subscriptionData = data;
} else {
this.subscriptionData = genInitialSubscribeDetails();
}
} catch (error) {
const notificationStore = useNotificationStore();
Expand Down
2 changes: 2 additions & 0 deletions app/src/forms/email/reminderService.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ const service = {
}),
];
}

return [];
},
_getGraceDate: (schedule) => {
let substartDate = moment(schedule.openSubmissionDateTime);
Expand Down
139 changes: 139 additions & 0 deletions app/src/forms/event/eventService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const log = require('../../components/log')(module.filename);
const { SubscriptionEvent } = require('../common/constants');
const { FormVersion, Form, FormSubscription } = require('../common/models');
const axios = require('axios');
const { queryUtils } = require('../common/utils');

const service = {
/**
* @function formSubmissionEventReceived
* Completing submission of a form
* @param {string} formId
* @param {string} formVersionId
* @param {string} submissionId
* @param {string} data
* @returns The callout to the submission event endpoint
*/
formSubmissionEventReceived: async (formId, formVersionId, submissionId, data) => {
try {
const { subscribe } = await service.readForm(formId, {});
if (subscribe && subscribe.enabled && data.submission.data.submit) {
const formVersion = await service.readVersion(formVersionId);
const subscribeConfig = await service.readFormSubscriptionDetails(formId);
const config = Object.assign({}, subscribe, subscribeConfig);
service.postSubscriptionEvent(config, formVersion, submissionId, SubscriptionEvent.FORM_SUBMITTED);
}
} catch (e) {
log.error(e.message, {
function: 'formSubmissionEventReceived',
formId: formId,
formVersionId: formVersionId,
submissionId: submissionId,
data: data,
});
throw e;
}
},

/**
* @function postSubscriptionEvent
* Post the subscription event
* @param {string} subscribe settings
* @param {string} formVersion form version json object
* @param {string} submissionId submission id
* @param {string} subscriptionEvent string value of the event
* @returns The FormVersion
*/
postSubscriptionEvent: async (subscribe, formVersion, submissionId, subscriptionEvent) => {
try {
// Check if there are endpoints subscribed for form submission event
if (subscribe && subscribe.endpointUrl) {
const axiosOptions = { timeout: 10000 };
const axiosInstance = axios.create(axiosOptions);
const jsonData = { formId: formVersion.formId, formVersion: formVersion.id, subscriptionEvent: subscriptionEvent };
if (submissionId != null) {
jsonData['submissionId'] = submissionId;
}

axiosInstance.interceptors.request.use(
(cfg) => {
cfg.headers = { [subscribe.key]: `${subscribe.endpointToken}` };
return Promise.resolve(cfg);
},
(error) => {
return Promise.reject(error);
}
);
axiosInstance.post(subscribe.endpointUrl, jsonData);
}
} catch (err) {
log.error(err.message, err, {
function: 'postSubscriptionEvent',
});
}
},

/**
* @function publishFormEvent
* Publish Event for form
* @param {string} formId the form id
* @param {string} formVersionId form version id
* @param {boolean} publish bool true or false - Published or Unpublished
*/
publishFormEvent: async (formId, formVersionId, publish) => {
const { subscribe } = await service.readForm(formId);

if (subscribe && subscribe.enabled) {
const subscribeConfig = await service.readFormSubscriptionDetails(formId);
const config = Object.assign({}, subscribe, subscribeConfig);
const formVersion = new FormVersion();
formVersion.id = formVersionId;
formVersion.formId = formId;

if (publish) {
service.postSubscriptionEvent(config, formVersion, null, SubscriptionEvent.FORM_PUBLISHED);
} else {
service.postSubscriptionEvent(config, formVersion, null, SubscriptionEvent.FORM_UNPUBLISHED);
}
}
},

/**
* @function readFormSubscriptionDetails
* @param {string} formId
* Get the current subscription settings for a form
* @returns The subscription settings for a form
*/
readFormSubscriptionDetails: (formId) => {
return FormSubscription.query().modify('filterFormId', formId).first();
},

/**
* @function readVersion
* Get the current FormVersion
* @param {string} formVersionId
* @returns The FormVersion
*/
readVersion: (formVersionId) => {
return FormVersion.query().findById(formVersionId).throwIfNotFound();
},
/**
* @function readForm
* Read Form
* @param {string} formId form id
* @param {object} params form query params
* @returns The Form object
*/
readForm: (formId, params = {}) => {
params = queryUtils.defaultActiveOnly(params);
return Form.query()
.findById(formId)
.modify('filterActive', params.active)
.allowGraph('[identityProviders,versions]')
.withGraphFetched('identityProviders(orderDefault)')
.withGraphFetched('versions(selectWithoutSchema, orderVersionDescending)')
.throwIfNotFound();
},
};

module.exports = service;
69 changes: 7 additions & 62 deletions app/src/forms/form/service.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const Problem = require('api-problem');
const { ref } = require('objection');
const { v4: uuidv4 } = require('uuid');
const { EmailTypes, SubscriptionEvent } = require('../common/constants');
const axios = require('axios');
const log = require('../../components/log')(module.filename);
const { EmailTypes } = require('../common/constants');
const eventService = require('../event/eventService');
const moment = require('moment');
const {
FileStorage,
Expand Down Expand Up @@ -383,21 +382,7 @@ const service = {
});

await trx.commit();

const { subscribe } = await service.readForm(formId);
if (subscribe && subscribe.enabled) {
const subscribeConfig = await service.readFormSubscriptionDetails(formId);
const config = Object.assign({}, subscribe, subscribeConfig);
const formVersion = new FormVersion();
formVersion.id = formVersionId;
formVersion.formId = formId;

if (publish) {
service.postSubscriptionEvent(config, formVersion, null, SubscriptionEvent.FORM_PUBLISHED);
} else {
service.postSubscriptionEvent(config, formVersion, null, SubscriptionEvent.FORM_UNPUBLISHED);
}
}
eventService.publishFormEvent(formId, formVersionId, publish);

// return the published form/version...
return await service.readPublishedForm(formId);
Expand Down Expand Up @@ -445,7 +430,7 @@ const service = {
let trx;
try {
const formVersion = await service.readVersion(formVersionId);
const { identityProviders, subscribe } = await service.readForm(formVersion.formId);
const { identityProviders } = await service.readForm(formVersion.formId);

trx = await FormSubmission.startTransaction();

Expand Down Expand Up @@ -498,13 +483,10 @@ const service = {
};

await FormSubmissionStatus.query(trx).insert(stObj);
if (subscribe && subscribe.enabled) {
const subscribeConfig = await service.readFormSubscriptionDetails(formVersion.formId);
const config = Object.assign({}, subscribe, subscribeConfig);
service.postSubscriptionEvent(config, formVersion, submissionId, SubscriptionEvent.FORM_SUBMITTED);
}
}

eventService.formSubmissionEventReceived(formVersion.formId, formVersion.id, submissionId, data);

// does this submission contain any file uploads?
// if so, we need to update the file storage records.
// use the schema to determine if there are uploads, fetch the ids from the submission data...
Expand Down Expand Up @@ -685,15 +667,7 @@ const service = {
await FormVersionDraft.query().deleteById(formVersionDraftId);
await trx.commit();

const { subscribe } = await service.readForm(formId);
if (subscribe && subscribe.enabled) {
const subscribeConfig = await service.readFormSubscriptionDetails(formId);
const config = Object.assign({}, subscribe, subscribeConfig);
const formVersion = new FormVersion();
formVersion.id = version.id;
formVersion.formId = formId;
service.postSubscriptionEvent(config, formVersion, null, SubscriptionEvent.FORM_DRAFT_PUBLISHED);
}
eventService.publishFormEvent(formId, version.id, version.published);

// return the published version...
return await service.readVersion(version.id);
Expand Down Expand Up @@ -796,35 +770,6 @@ const service = {
return { url: imageUrl };
},

postSubscriptionEvent: async (subscribe, formVersion, submissionId, subscriptionEvent) => {
try {
// Check if there are endpoints subscribed for form submission event
if (subscribe && subscribe.endpointUrl) {
const axiosOptions = { timeout: 10000 };
const axiosInstance = axios.create(axiosOptions);
const jsonData = { formId: formVersion.formId, formVersion: formVersion.id, subscriptionEvent: subscriptionEvent };
if (submissionId != null) {
jsonData['submissionId'] = submissionId;
}

axiosInstance.interceptors.request.use(
(cfg) => {
cfg.headers = { [subscribe.key]: `${subscribe.endpointToken}` };
return Promise.resolve(cfg);
},
(error) => {
return Promise.reject(error);
}
);
axiosInstance.post(subscribe.endpointUrl, jsonData);
}
} catch (err) {
log.error(err.message, err, {
function: 'postSubscriptionEvent',
});
}
},

/**
* @function listFormComponentsProactiveHelp
* Search for all form components help information
Expand Down
3 changes: 2 additions & 1 deletion app/src/forms/submission/service.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { v4: uuidv4 } = require('uuid');

const { Statuses } = require('../common/constants');
const { Form, FormVersion, FormSubmission, FormSubmissionStatus, Note, SubmissionAudit, SubmissionMetadata } = require('../common/models');
const emailService = require('../email/emailService');
const eventService = require('../event/eventService');
const formService = require('../form/service');
const permissionService = require('../permission/service');

Expand Down Expand Up @@ -72,6 +72,7 @@ const service = {
// If finalizing submission, send the submission email (quiet fail if anything goes wrong)
const submissionMetaData = await SubmissionMetadata.query().where('submissionId', formSubmissionId).first();
emailService.submissionReceived(submissionMetaData.formId, formSubmissionId, data, referrer).catch(() => {});
eventService.formSubmissionEventReceived(submissionMetaData.formId, submissionMetaData.formVersionId, formSubmissionId, data);
}
} else {
if (statuses && statuses.length > 0 && (statuses[0].code === Statuses.SUBMITTED || statuses[0].code === Statuses.COMPLETED)) {
Expand Down
10 changes: 10 additions & 0 deletions app/tests/unit/forms/email/reminderService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ const schedule = {
closeSubmissionDateTime: null,
};

describe('_init', () => {
it('should handle reminder enabled with empty schedule', async () => {
reminderService._getForms = jest.fn().mockReturnValue([{ schedule: {} }]);

const result = await reminderService._init();

expect(result.errors).not.toBe([]);
});
});

describe('getDifference', () => {
it('should return the difference between 2 arrays of object', () => {
let obj1 = [
Expand Down
6 changes: 5 additions & 1 deletion openshift/app.dc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ objects:
app.openshift.io/connects-to: '[{"apiVersion":"apps/v1","kind":"StatefulSet","name":"patroni-${JOB_NAME}"}]'
name: "${APP_NAME}-app-${JOB_NAME}"
spec:
replicas: 2
replicas: ${{REPLICAS}}
revisionHistoryLimit: 10
selector:
app: "${APP_NAME}-${JOB_NAME}"
Expand Down Expand Up @@ -361,3 +361,7 @@ parameters:
description: Requested Memory per pod (in gigabytes Gi or megabytes Mi ex. 500Mi)
displayName: Memory Request
value: 256Mi
- name: REPLICAS
description: The number of replicas to run
displayName: Replicas
value: "2"
1 change: 1 addition & 0 deletions openshift/app.prod.param
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CPU_LIMIT=1500m
CPU_REQUEST=250m
MEMORY_LIMIT=2Gi
REPLICAS=3

0 comments on commit 9e579a8

Please sign in to comment.