Skip to content

Commit 32148b1

Browse files
feat: allow draft edition of records
* feat/AB#65023-grid-apply-record-rules * add comment * refactor some code --------- Co-authored-by: Antoine Hurard <[email protected]>
1 parent 1fedb51 commit 32148b1

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

src/schema/mutation/editRecord.mutation.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
GraphQLID,
44
GraphQLError,
55
GraphQLString,
6+
GraphQLBoolean,
67
} from 'graphql';
78
import GraphQLJSON from 'graphql-type-json';
89
import { Form, Record, Resource, Version } from '@models';
@@ -11,6 +12,7 @@ import {
1112
transformRecord,
1213
getOwnership,
1314
checkRecordValidation,
15+
checkRecordTriggers,
1416
} from '@utils/form';
1517
import { RecordType } from '../types';
1618
import mongoose from 'mongoose';
@@ -63,6 +65,7 @@ export default {
6365
version: { type: GraphQLID },
6466
template: { type: GraphQLID },
6567
lang: { type: GraphQLString },
68+
draft: { type: GraphQLBoolean },
6669
},
6770
async resolve(parent, args, context) {
6871
try {
@@ -101,6 +104,17 @@ export default {
101104
);
102105
}
103106

107+
// If draft option, return record after running triggers
108+
if (args.draft) {
109+
const triggeredRecord = checkRecordTriggers(
110+
oldRecord,
111+
args.data,
112+
parentForm,
113+
context
114+
);
115+
return triggeredRecord;
116+
}
117+
104118
// Update record
105119
// Put a try catch for record validation + check the structure of this form
106120
let validationErrors;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Form, Record } from 'models';
2+
import * as Survey from 'survey-knockout';
3+
4+
/**
5+
* Check record triggered values, so inline edition ( draft edition ) can indicate changes on the data
6+
*
7+
* @param record edited record
8+
* @param newData record updated data
9+
* @param form template to use
10+
* @param context graphQLContext
11+
* @returns New record data
12+
*/
13+
export const checkRecordTriggers = (
14+
record: Record,
15+
newData: any,
16+
form: Form,
17+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
18+
context
19+
): Record => {
20+
// Necessary to fix 401 errors if we have choicesByUrl targeting self API.
21+
// passTokenForChoicesByUrl(context);
22+
// Avoid the choices by url to be called, as it could freeze system depending on the choices
23+
(Survey.ChoicesRestful as any).getCachedItemsResult = () => true;
24+
const survey = new Survey.Model(form.structure);
25+
Survey.settings.commentPrefix = '_comment';
26+
survey.data = { ...record.data, ...newData };
27+
const triggers = survey.toJSON().triggers;
28+
if (triggers) {
29+
survey.runTriggers();
30+
}
31+
const updatedRecord = new Record(record);
32+
updatedRecord.data = survey.data;
33+
return updatedRecord;
34+
};

src/utils/form/checkRecordValidation.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import { Form, Record } from 'models';
22
import * as Survey from 'survey-knockout';
3-
import config from 'config';
3+
// import config from 'config';
44

55
/**
66
* Pass token before the request to fetch choices by URL if it's targeting SAFE API
77
*
88
* @param context GraphQL context.
99
*/
10-
export const passTokenForChoicesByUrl = (context: any) => {
11-
Survey.ChoicesRestfull.onBeforeSendRequest = (
12-
sender: Survey.ChoicesRestful,
13-
options: { request: XMLHttpRequest }
14-
) => {
15-
if (sender.url.includes(config.get('server.url'))) {
16-
const token = context.token;
17-
options.request.setRequestHeader('Authorization', token);
18-
}
19-
};
20-
};
10+
// export const passTokenForChoicesByUrl = (context: any) => {
11+
// Survey.ChoicesRestfull.onBeforeSendRequest = (
12+
// sender: Survey.ChoicesRestful,
13+
// options: { request: XMLHttpRequest }
14+
// ) => {
15+
// if (sender.url.includes(config.get('server.url'))) {
16+
// const token = context.token;
17+
// options.request.setRequestHeader('Authorization', token);
18+
// }
19+
// };
20+
// };
2121

2222
/**
2323
* Check if the record is correct according to the defined surveyjs validators
2424
*
2525
* @param record The record to check
2626
* @param newData The proposed update
2727
* @param form The formulaire object linked to the record
28-
* @param context GraphQL context
28+
* @param context graphQL context
2929
* @param lang The current language of the form
3030
* @returns The list of errors (empty if no errors)
3131
*/

src/utils/form/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export * from './getNextId';
1313
export * from './getDisplayText';
1414
export * from './checkRecordValidation';
1515
export * from './getAccessibleFields';
16+
export * from './checkRecordTriggers';

0 commit comments

Comments
 (0)