Skip to content

Commit

Permalink
Add in publish form event subscription (bcgov#1180)
Browse files Browse the repository at this point in the history
* Add in publish form event subscription (bcgov#1146)

* 933 publish event subscription

* feat/933 Event subscription add publish draft and unpublish events

* feat/933 Event subscription add publish draft and unpublish events

* eat/933 Event subscription add publish draft and unpublish events -remove test file

* feat/933 Event subscription add publish draft and unpublish events -remove test file move post subscribe into if not draft

---------

Co-authored-by: Walter Moar <[email protected]>

---------

Co-authored-by: jimmyPasta <[email protected]>
Co-authored-by: Walter Moar <[email protected]>
  • Loading branch information
3 people authored Jan 25, 2024
1 parent 5855386 commit 7cb9c4e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/src/forms/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ module.exports = Object.freeze({
FORM_SUBMITTED: 'eventSubmission',
FORM_STATUS_CHANGE: 'eventStatusChange',
FORM_ASSIGNMENT: 'eventAssignment',
FORM_PUBLISHED: 'eventFormPublished',
FORM_DRAFT_PUBLISHED: 'eventFormDraftPublished',
FORM_UNPUBLISHED: 'eventFormUnPublished',
},
StorageTypes: {
UPLOADS: 'uploads',
Expand Down
41 changes: 34 additions & 7 deletions app/src/forms/form/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,21 @@ 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);
}
}

// return the published form/version...
return await service.readPublishedForm(formId);
} catch (err) {
Expand Down Expand Up @@ -483,11 +498,11 @@ 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);
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);
}
}

// does this submission contain any file uploads?
Expand Down Expand Up @@ -670,6 +685,16 @@ 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);
}

// return the published version...
return await service.readVersion(version.id);
} catch (err) {
Expand Down Expand Up @@ -748,7 +773,10 @@ const service = {
if (subscribe && subscribe.endpointUrl) {
const axiosOptions = { timeout: 10000 };
const axiosInstance = axios.create(axiosOptions);
const jsonData = { formId: formVersion.formId, formVersion: formVersion.id, submissionId: submissionId, subscriptionEvent: subscriptionEvent };
const jsonData = { formId: formVersion.formId, formVersion: formVersion.id, subscriptionEvent: subscriptionEvent };
if (submissionId != null) {
jsonData['submissionId'] = submissionId;
}

axiosInstance.interceptors.request.use(
(cfg) => {
Expand All @@ -759,7 +787,6 @@ const service = {
return Promise.reject(error);
}
);

axiosInstance.post(subscribe.endpointUrl, jsonData);
}
} catch (err) {
Expand Down

0 comments on commit 7cb9c4e

Please sign in to comment.