fix: cast file owners in atomic upserts

This commit is contained in:
Marco Beretta 2026-07-26 19:49:03 +02:00
parent 64a32166e0
commit 2cdd88133d
No known key found for this signature in database
GPG key ID: D918033D8E74CC11
2 changed files with 20 additions and 0 deletions

View file

@ -90,6 +90,23 @@ describe('File Methods', () => {
expect(file?.expiresAt).toBeUndefined();
});
it('casts string owner ids in atomic pipeline upserts', async () => {
const fileId = uuidv4();
const userId = new mongoose.Types.ObjectId();
const file = await fileMethods.createFile({
file_id: fileId,
user: userId.toString() as unknown as mongoose.Types.ObjectId,
filename: 'owned.txt',
filepath: '/uploads/owned.txt',
type: 'text/plain',
bytes: 100,
});
expect(file?.user).toEqual(userId);
await expect(File.countDocuments({ file_id: fileId, user: userId })).resolves.toBe(1);
});
it('updates expiredAt monotonically with an atomic minimum', async () => {
const fileId = uuidv4();
const userId = new mongoose.Types.ObjectId();

View file

@ -368,6 +368,9 @@ export function createFileMethods(mongoose: typeof import('mongoose')): {
const definedFields = Object.fromEntries(
Object.entries(fields).filter(([, value]) => value !== undefined),
);
if (definedFields.user != null) {
definedFields.user = File.schema.path('user').cast(definedFields.user);
}
const insertDefaults = {
object: { $ifNull: ['$object', 'file'] },
usage: { $ifNull: ['$usage', 0] },