Refactored editObject function to flatten object ID references before updating the database, ensuring proper data structure. Added corresponding test case to verify the flattening functionality in update operations.
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
This commit is contained in:
parent
491900bdaf
commit
82ca27756d
@ -14,6 +14,23 @@ jest.unstable_mockModule('../../utils.js', () => ({
|
|||||||
editAuditLog: jest.fn(),
|
editAuditLog: jest.fn(),
|
||||||
editNotification: jest.fn(),
|
editNotification: jest.fn(),
|
||||||
expandObjectIds: jest.fn((obj) => obj),
|
expandObjectIds: jest.fn((obj) => obj),
|
||||||
|
flatternObjectIds: jest.fn((object) => {
|
||||||
|
if (!object || typeof object !== 'object') {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = {};
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(object)) {
|
||||||
|
if (value && typeof value === 'object' && value._id) {
|
||||||
|
result[key] = value._id;
|
||||||
|
} else {
|
||||||
|
result[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}),
|
||||||
getFieldsByRef: jest.fn(() => []),
|
getFieldsByRef: jest.fn(() => []),
|
||||||
getQueryToCacheKey: jest.fn(({ model, id }) => `${model}:${id}`),
|
getQueryToCacheKey: jest.fn(({ model, id }) => `${model}:${id}`),
|
||||||
modelHasRef: jest.fn(() => false),
|
modelHasRef: jest.fn(() => false),
|
||||||
@ -147,6 +164,19 @@ describe('Database Utilities (CRUD)', () => {
|
|||||||
expect(mockModel.findByIdAndUpdate).toHaveBeenCalledWith(id, updateData);
|
expect(mockModel.findByIdAndUpdate).toHaveBeenCalledWith(id, updateData);
|
||||||
expect(result).toEqual({ ...previousData, ...updateData });
|
expect(result).toEqual({ ...previousData, ...updateData });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should flatten object id references before updating', async () => {
|
||||||
|
const id = '123';
|
||||||
|
const fileId = '507f1f77bcf86cd799439012';
|
||||||
|
const updateData = { file: { _id: fileId, name: 'part.gcode' } };
|
||||||
|
const previousData = { _id: id, name: 'Old' };
|
||||||
|
|
||||||
|
mockModel.lean.mockResolvedValue(previousData);
|
||||||
|
|
||||||
|
await editObject({ model: mockModel, id, updateData });
|
||||||
|
|
||||||
|
expect(mockModel.findByIdAndUpdate).toHaveBeenCalledWith(id, { file: fileId });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('deleteObject', () => {
|
describe('deleteObject', () => {
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import {
|
|||||||
distributeStats,
|
distributeStats,
|
||||||
editNotification,
|
editNotification,
|
||||||
deleteNotification,
|
deleteNotification,
|
||||||
|
flatternObjectIds,
|
||||||
} from '../utils.js';
|
} from '../utils.js';
|
||||||
import { getAllModels } from '../services/misc/model.js';
|
import { getAllModels } from '../services/misc/model.js';
|
||||||
import { redisServer } from './redis.js';
|
import { redisServer } from './redis.js';
|
||||||
@ -785,7 +786,7 @@ export const editObject = async ({ model, id, updateData, user, populate, recalc
|
|||||||
// Determine parentType from model name
|
// Determine parentType from model name
|
||||||
const parentType = model.modelName ? model.modelName : 'unknown';
|
const parentType = model.modelName ? model.modelName : 'unknown';
|
||||||
// Fetch the and update object
|
// Fetch the and update object
|
||||||
var query = model.findByIdAndUpdate(id, updateData).lean();
|
var query = model.findByIdAndUpdate(id, flatternObjectIds(updateData)).lean();
|
||||||
|
|
||||||
if (populate) {
|
if (populate) {
|
||||||
if (Array.isArray(populate)) {
|
if (Array.isArray(populate)) {
|
||||||
|
|||||||
@ -122,7 +122,7 @@ describe('slicer upload', () => {
|
|||||||
updateData: {
|
updateData: {
|
||||||
pendingSlicerUploads: [
|
pendingSlicerUploads: [
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
file: storedFileId,
|
file: { _id: storedFileId },
|
||||||
shouldPrint: false,
|
shouldPrint: false,
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user