From cc5dbee5be0aa6017336b7f65cb7ba7a859f1098 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Wed, 1 Jul 2026 18:52:43 +0200 Subject: [PATCH] fixed assets uploading issue --- backend/src/db/api/base.api.ts | 10 ++++-- backend/src/index.ts | 12 ++++++- backend/tests/update-contracts.test.ts | 48 +++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/backend/src/db/api/base.api.ts b/backend/src/db/api/base.api.ts index c0f9848..2256909 100644 --- a/backend/src/db/api/base.api.ts +++ b/backend/src/db/api/base.api.ts @@ -51,6 +51,7 @@ interface GenericDbFindAndCountOptions { type FieldTransformer = (value: unknown) => unknown; type RelationSetter = ( + this: EntityRecord, value: unknown, options: { transaction?: Transaction | undefined }, ) => Promise; @@ -322,7 +323,8 @@ class GenericDBApi { if (data[assoc.field] !== undefined) { const setter = record[assoc.setter]; if (isRelationSetter(setter)) { - await setter( + await setter.call( + record, data[assoc.field] || (assoc.isArray ? [] : null), buildTransactionOptions(transaction), ); @@ -393,7 +395,11 @@ class GenericDBApi { if (data[assoc.field] !== undefined) { const setter = record[assoc.setter]; if (isRelationSetter(setter)) { - await setter(data[assoc.field], buildTransactionOptions(transaction)); + await setter.call( + record, + data[assoc.field], + buildTransactionOptions(transaction), + ); } } } diff --git a/backend/src/index.ts b/backend/src/index.ts index a6fd557..015a6bc 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -431,11 +431,21 @@ function getServerPort(): number { const PORT = getServerPort(); -app.listen(PORT, () => { +const server = app.listen(PORT, () => { logger.info( { port: PORT, env: process.env.NODE_ENV || 'development' }, 'Server started', ); }); +server.on('error', (err: NodeJS.ErrnoException) => { + logger.error( + { err, port: PORT, env: process.env.NODE_ENV || 'development' }, + 'Server failed to start', + ); + setImmediate(() => { + process.exit(1); + }); +}); + export default app; diff --git a/backend/tests/update-contracts.test.ts b/backend/tests/update-contracts.test.ts index 59e1884..2795065 100644 --- a/backend/tests/update-contracts.test.ts +++ b/backend/tests/update-contracts.test.ts @@ -51,6 +51,7 @@ interface UpdateContractCalls { serviceCreate?: unknown; serviceDeleteByIds?: unknown; serviceRemove?: unknown; + associationThis?: TestAssociationRecord; committed?: true; rolledBack?: true; commits?: number; @@ -139,7 +140,8 @@ void test('GenericDBApi.update uses object signature and forwards update context calls.update = { payload, options }; return Promise.resolve(); }, - setTags(value, options) { + setTags(this: TestAssociationRecord, value, options) { + calls.associationThis = this; calls.setTags = { value, options }; return Promise.resolve(); }, @@ -185,6 +187,7 @@ void test('GenericDBApi.update uses object signature and forwards update context value: ['a', 'b'], options: {}, }); + assert.equal(calls.associationThis, record); }); void test('GenericDBApi.update rejects positional signature', async () => { @@ -256,6 +259,49 @@ void test('GenericDBApi.create uses object signature and forwards create context }); }); +void test('GenericDBApi.create calls association setters with record context', async () => { + const calls: UpdateContractCalls = {}; + const record: TestAssociationRecord = { + id: 'record-1', + update() { + return Promise.resolve(); + }, + setTags(this: TestAssociationRecord, value, options) { + calls.associationThis = this; + calls.setTags = { value, options }; + return Promise.resolve(); + }, + }; + + class TestDBApi extends GenericDBApi { + static override get MODEL(): unknown { + return { + rawAttributes: {}, + getTableName: () => 'test_records', + create(payload: DbData, options: TestTransactionOptions) { + calls.create = { payload, options }; + return Promise.resolve(record); + }, + }; + } + + static override get ASSOCIATIONS() { + return [{ field: 'tags', setter: 'setTags', isArray: true }]; + } + } + + await TestDBApi.create({ + data: { name: 'Created', tags: ['a'] }, + currentUser: createCurrentUser(), + }); + + assert.equal(calls.associationThis, record); + assert.deepEqual(calls.setTags, { + value: ['a'], + options: {}, + }); +}); + void test('GenericDBApi.create rejects positional signature', async () => { class TestDBApi extends GenericDBApi { static override get MODEL(): unknown {