Skip to content

Commit

Permalink
Add 'archived' field to defaultRecordFields and tweak pci custom scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus-relief committed Apr 2, 2024
1 parent 4082dcf commit aa73871
Show file tree
Hide file tree
Showing 10 changed files with 886 additions and 1,042 deletions.
1,841 changes: 801 additions & 1,040 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/const/defaultRecordFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export const defaultRecordFields: {
$toString: '$_id',
},
},
{
field: 'archived',
type: GraphQLBoolean,
filterType: GraphQLBoolean,
selectable: true,
},
{
field: 'incrementalId',
type: GraphQLID,
Expand Down
1 change: 1 addition & 0 deletions src/schema/query/recordsAggregation.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const DEFAULT_FIELDS = [
'lastUpdateForm',
'createdAt',
'createdBy',
'archived',
'modifiedAt',
'id',
'incrementalId',
Expand Down
4 changes: 4 additions & 0 deletions src/utils/files/getColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const getColumns = async (
): Promise<any[]> => {
const columns = [];
for (const field of fields) {
// Skip fields that should be omitted on the xlsx template.
if (field.showOnXlsxTemplate === false) {
continue;
}
switch (field.type) {
case 'checkbox':
case 'tagbox': {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/files/getUploadColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
export const getUploadColumns = (fields: any[], headers: any[]): any[] => {
const columns = [];
for (const field of fields) {
// Skip fields that should be omitted on the xlsx template.
if (field.showOnXlsxTemplate === false) {
continue;
}
switch (field.type) {
case 'checkbox':
case 'tagbox': {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/form/extractFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { validateGraphQLFieldName } from '@utils/validators';
export const extractFields = async (object, fields, core): Promise<void> => {
if (object.elements) {
for (const element of object.elements) {
if (element.omitField) continue;
if (element.omitField) {
continue;
}
if (element.type === 'panel') {
await extractFields(element, fields, core);
} else {
Expand All @@ -29,12 +31,14 @@ export const extractFields = async (object, fields, core): Promise<void> => {
type,
name: element.valueName,
isRequired: element.isRequired ? element.isRequired : false,
showOnXlsxTemplate: !element.omitOnXlsxTemplate,
readOnly: element.readOnly ? element.readOnly : false,
isCore: core,
...(element.hasOwnProperty('defaultValue')
? { defaultValue: element.defaultValue }
: {}),
};
console.log(field.name, field);
// ** Resource **
if (element.type === 'resource' || element.type === 'resources') {
if (element.relatedName) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/form/metadata.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ export const getMetaData = async (
editor: 'datetime',
canUpdate: false,
});

// Archived
metaData.push({
automated: true,
name: 'archived',
type: 'boolean',
editor: 'boolean',
canUpdate: false,
});
}

/**
Expand Down
52 changes: 51 additions & 1 deletion src/utils/pci/setupCustomListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Schema, Types } from 'mongoose';
import config from 'config';
import { Form, Record } from '@models';
import { getNextId } from '@utils/form';
import { cloneDeep } from 'lodash';

/** Whether or not the current environment is PCI */
const IS_PCI = config.get('server.url') === 'https://pci.oortcloud.tech/api';
const IS_PCI = config.get('server.url') === 'https://ltkmp.oortcloud.tech/api';

/** Location form id */
const LOCATION_FORM_ID = new Types.ObjectId('65d61bfbccb8d36936d10cb6');
Expand Down Expand Up @@ -49,6 +50,10 @@ const createTensionIndicatorEntry = async (record: Record) => {
label: 'High tension',
},
};
const skip = !!record.data?.__automated;
if (skip) {
return;
}
const status = record.data?.status;

const entryForm = await Form.findById(INDICATOR_ENTRY_FORM_ID);
Expand All @@ -70,6 +75,7 @@ const createTensionIndicatorEntry = async (record: Record) => {
qualitative_value: statusMap[status].label,
unit: 'Scale (1-3)',
location: record._id.toString(),
__automated: true,
},
_form: {
_id: entryForm._id,
Expand Down Expand Up @@ -98,6 +104,50 @@ export const setupCustomPCIListeners = <DocType>(schema: Schema<DocType>) => {
const rec = doc as Record;
if (LOCATION_FORM_ID.equals(rec.form)) {
await createTensionIndicatorEntry(rec);
} else if (INDICATOR_ENTRY_FORM_ID.equals(rec.form)) {
// we should update the location status from the indicator entry
const { location: locationID, indicator, date } = rec.data;

const newStatus =
rec.data.value === 1
? 'low'
: rec.data.value === 2
? 'moderate'
: 'high';

// we now check if there is a newer entry for the same location and indicator
const moreRecentEntry = await Record.exists({
form: INDICATOR_ENTRY_FORM_ID,
'data.location': locationID,
'data.indicator': indicator,
'data.date': { $gte: date },
archived: false,
}).sort({ createdAt: -1 });

const ind = await Record.findById(indicator);
const correctUnit = await Record.findById(ind.data.unit);

// If the unit is incorrect, we update it
if (correctUnit.data?.unit !== rec.data.unit) {
rec.data.unit = correctUnit.data?.unit;
rec.markModified('data');
await rec.save();
}

// If adding an older entry, we should not update the location status
// as that displays the most recent status
if (moreRecentEntry && !moreRecentEntry._id.equals(rec._id)) {
return;
}

// we update the location status
const location = await Record.findOne({
_id: locationID,
});
const newData = cloneDeep(location.data) || {};
Object.assign(newData, { status: newStatus, __automated: true });
location.data = newData;
await location.save();
}
});

Expand Down
1 change: 1 addition & 0 deletions src/utils/schema/resolvers/Query/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ export default (entityName: string, fieldsByName: any, idsByName: any) =>
[`_${resource}.id`]: { $toString: `$_${resource}._id` },
[`_${resource}.data.id`]: { $toString: `$_${resource}._id` },
[`_${resource}.data._id`]: { $toString: `$_${resource}._id` },
[`_${resource}.data.archived`]: `$_${resource}.archived`,
},
},
]);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/schema/resolvers/Query/getFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const DEFAULT_FIELDS = [
name: 'lastUpdateForm',
type: 'text',
},
{
name: 'archived',
type: 'boolean',
},
];

/** Maps oort simple operators to their mongo counterparts */
Expand Down

0 comments on commit aa73871

Please sign in to comment.