Skip to content

Commit df40673

Browse files
authored
Merge pull request #398 from devforth/AdminForth/929
fix: fix isUnique resource constraint for the records edit
2 parents 9c43bee + 628eef8 commit df40673

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

adminforth/dataConnectors/baseConnector.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,19 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
227227
throw new Error('Method not implemented.');
228228
}
229229

230-
async checkUnique(resource: AdminForthResource, column: AdminForthResourceColumn, value: any) {
230+
async checkUnique(resource: AdminForthResource, column: AdminForthResourceColumn, value: any, record?: any): Promise<boolean> {
231231
process.env.HEAVY_DEBUG && console.log('☝️🪲🪲🪲🪲 checkUnique|||', column, value);
232+
233+
const primaryKeyField = this.getPrimaryKey(resource);
232234
const existingRecord = await this.getData({
233235
resource,
234-
filters: { operator: AdminForthFilterOperators.AND, subFilters: [{ field: column.name, operator: AdminForthFilterOperators.EQ, value }]},
236+
filters: {
237+
operator: AdminForthFilterOperators.AND,
238+
subFilters: [
239+
{ field: column.name, operator: AdminForthFilterOperators.EQ, value },
240+
...(record ? [{ field: primaryKeyField, operator: AdminForthFilterOperators.NE as AdminForthFilterOperators.NE, value: record[primaryKeyField] }] : [])
241+
]
242+
},
235243
limit: 1,
236244
sort: [],
237245
offset: 0,
@@ -306,7 +314,7 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
306314
async updateRecord({ resource, recordId, newValues }: { resource: AdminForthResource; recordId: string; newValues: any; }): Promise<{ error?: string; ok: boolean; }> {
307315
// transform value using setFieldValue and call updateRecordOriginalValues
308316
const recordWithOriginalValues = {...newValues};
309-
317+
310318
for (const field of Object.keys(newValues)) {
311319
const col = resource.dataSourceColumns.find((col) => col.name == field);
312320
// todo instead of throwing error, we can just not use setFieldValue here, and pass original value to updateRecordOriginalValues
@@ -319,6 +327,23 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
319327
}
320328
recordWithOriginalValues[col.name] = this.setFieldValue(col, newValues[col.name]);
321329
}
330+
const record = await this.getRecordByPrimaryKey(resource, recordId);
331+
let error: string | null = null;
332+
await Promise.all(
333+
resource.dataSourceColumns.map(async (col) => {
334+
if (col.isUnique && !col.virtual && !error) {
335+
const exists = await this.checkUnique(resource, col, recordWithOriginalValues[col.name], record);
336+
if (exists) {
337+
error = `Record with ${col.name} ${recordWithOriginalValues[col.name]} already exists`;
338+
}
339+
}
340+
})
341+
);
342+
if (error) {
343+
process.env.HEAVY_DEBUG && console.log('🪲🆕 check unique error', error);
344+
return { error, ok: false };
345+
}
346+
322347

323348
process.env.HEAVY_DEBUG && console.log(`🪲✏️ updating record id:${recordId}, values: ${JSON.stringify(recordWithOriginalValues)}`);
324349

adminforth/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,10 @@ class AdminForth implements IAdminForth {
651651
}
652652

653653
if (Object.keys(newValues).length > 0) {
654-
await connector.updateRecord({ resource, recordId, newValues });
654+
const { error } = await connector.updateRecord({ resource, recordId, newValues });
655+
if ( error ) {
656+
return { error };
657+
}
655658
}
656659

657660
// execute hook if needed

adminforth/spa/src/views/EditView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ async function saveRecord(opts?: { confirmationResult?: any }) {
218218
variant: 'success',
219219
timeout: 400000
220220
});
221+
router.push({ name: 'resource-show', params: { resourceId: route.params.resourceId, primaryKey: resp.recordId } });
221222
}
222223
saving.value = false;
223-
router.push({ name: 'resource-show', params: { resourceId: route.params.resourceId, primaryKey: resp.recordId } });
224224
}
225225
226226
</script>

0 commit comments

Comments
 (0)