Skip to content

Commit

Permalink
Merge pull request #38 from ReliefApplications/feat/HIT-261-unique-check
Browse files Browse the repository at this point in the history
feat/HIT-261-unique-check
  • Loading branch information
matheus-relief authored Apr 2, 2024
2 parents aa73871 + 8fec00d commit 35a3633
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
"invalidArguments": "Either data or version must be provided.",
"wrongTemplateProvided": "Template must sharing the same resource as the parent form of the edited record."
}
},
"errors": {
"missingParameters": "Missing query parameters: record id or uniqueField and uniqueValue"
}
},
"reference": {
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
"invalidArguments": "******",
"wrongTemplateProvided": "******"
}
},
"errors": {
"missingParameters": "******"
}
},
"reference": {
Expand Down
40 changes: 32 additions & 8 deletions src/schema/mutation/editRecord.mutation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
GraphQLNonNull,
GraphQLID,
GraphQLError,
GraphQLString,
Expand Down Expand Up @@ -57,7 +56,9 @@ export const hasInaccessibleFields = (

/** Arguments for the editRecord mutation */
type EditRecordArgs = {
id: string | Types.ObjectId;
id?: string | Types.ObjectId;
uniqueField?: string;
uniqueValue?: string;
data?: any;
version?: string | Types.ObjectId;
template?: string | Types.ObjectId;
Expand All @@ -72,7 +73,9 @@ type EditRecordArgs = {
export default {
type: RecordType,
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
id: { type: GraphQLID },
uniqueField: { type: GraphQLString },
uniqueValue: { type: GraphQLString },
data: { type: GraphQLJSON },
version: { type: GraphQLID },
template: { type: GraphQLID },
Expand All @@ -90,10 +93,27 @@ export default {
);
}

const user = context.user;
if (!(args.uniqueField && args.uniqueValue) && !args.id) {
throw new GraphQLError(
context.i18next.t('mutations.record.errors.missingParameters')
);
}

// Get record and form
const oldRecord: Record = await Record.findById(args.id);
const user = context.user;
// Get record
let oldRecord: Record;
if (args.id) {
oldRecord = await Record.findById(args.id);
} else if (args.uniqueField && args.uniqueValue) {
const query = {};
query[`data.${args.uniqueField}`] = args.uniqueValue;
oldRecord = await Record.findOne(query);
}
if (!oldRecord) {
throw new GraphQLError(context.i18next.t('common.errors.dataNotFound'));
}
const oldRecordId = oldRecord.id;
// Get form
const parentForm: Form = await Form.findById(
oldRecord.form,
'fields permissions resource structure'
Expand Down Expand Up @@ -203,7 +223,9 @@ export default {
update,
ownership && { createdBy: { ...oldRecord.createdBy, ...ownership } }
);
const record = Record.findByIdAndUpdate(args.id, update, { new: true });
const record = Record.findByIdAndUpdate(oldRecordId, update, {
new: true,
});
await version.save();
return await record;
} else {
Expand Down Expand Up @@ -234,7 +256,9 @@ export default {
},
$push: { versions: version._id },
};
const record = Record.findByIdAndUpdate(args.id, update, { new: true });
const record = Record.findByIdAndUpdate(oldRecordId, update, {
new: true,
});
await version.save();
return await record;
}
Expand Down
30 changes: 26 additions & 4 deletions src/schema/query/record.query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLNonNull, GraphQLID, GraphQLError } from 'graphql';
import { GraphQLID, GraphQLError, GraphQLString } from 'graphql';
import { Form, Record } from '@models';
import { RecordType } from '../types';
import extendAbilityForRecords from '@security/extendAbilityForRecords';
Expand All @@ -10,7 +10,9 @@ import { Context } from '@server/apollo/context';

/** Arguments for the record query */
type RecordArgs = {
id: string | Types.ObjectId;
id?: string | Types.ObjectId;
uniqueField?: string;
uniqueValue?: string;
};

/**
Expand All @@ -20,14 +22,34 @@ type RecordArgs = {
export default {
type: RecordType,
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
id: { type: GraphQLID },
uniqueField: { type: GraphQLString },
uniqueValue: { type: GraphQLString },
},
async resolve(parent, args: RecordArgs, context: Context) {
graphQLAuthCheck(context);
try {
const user = context.user;
// Get the form and the record
const record = await Record.findById(args.id);
if (!(args.uniqueField && args.uniqueValue) && !args.id) {
throw new GraphQLError(
context.i18next.t('mutations.record.errors.missingParameters')
);
}

let record: Record;
if (args.id) {
record = await Record.findById(args.id);
} else if (args.uniqueField && args.uniqueValue) {
const query = {};
query[`data.${args.uniqueField}`] = args.uniqueValue;
record = await Record.findOne(query);
}

if (!record) {
throw new GraphQLError(context.i18next.t('common.errors.dataNotFound'));
}

const form = await Form.findById(record.form);

// Check ability
Expand Down

0 comments on commit 35a3633

Please sign in to comment.