Skip to content

Commit 543aeec

Browse files
committed
[Fix] User setter
1 parent 997587f commit 543aeec

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

libs/api/core/src/query/module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { NestjsQueryCoreModule } from '@owl-app/nestjs-query-core';
66
import { NestjsQueryTypeOrmModule } from '@owl-app/nestjs-query-typeorm';
77
import { RegistryServiceModule } from '@owl-app/registry-nestjs';
88

9+
import BaseEntity from '../database/entity/base.entity';
910
import { createPaginatedQueryServiceProvider } from '../data-provider/query/providers';
1011
import { PaginationConfigProvider } from '../config/pagination';
1112
import { TypeOrmModule } from '../typeorm/typeorm.module';
@@ -99,7 +100,7 @@ export class AppNestjsQueryTypeOrmModule {
99100
name: SETTER_REGISTRY_TENANT,
100101
services: {
101102
tenant: TenantRelationSetter<TenantAware>,
102-
user: OwnerRelationSetter<UserAware>,
103+
user: OwnerRelationSetter<UserAware & BaseEntity>,
103104
},
104105
}),
105106
],

libs/api/core/src/query/typeorm/services/app-typeorm-query.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ export class AppTypeOrmQueryService<Entity>
190190
): Promise<Entity> {
191191
this.ensureIdIsNotPresent(update);
192192

193-
this.injectSetters(update);
194-
195193
let entity: Entity = null;
196194

197195
if (typeof id === 'object') {
@@ -200,6 +198,8 @@ export class AppTypeOrmQueryService<Entity>
200198
entity = await this.getById(id, opts);
201199
}
202200

201+
this.injectSetters(entity as DeepPartial<Entity>);
202+
203203
this.copyRegularColumn(entity, update);
204204

205205
if (entity instanceof DomainEventableEntity) {

libs/api/core/src/typeorm/app-typeorm.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AppTypeOrmOpts } from './types';
99
import { DEFAULT_DATA_SOURCE_NAME } from '../contants';
1010
import { TypeOrmModule } from './typeorm.module';
1111

12+
import BaseEntity from '../database/entity/base.entity';
1213
import { FILTER_REGISTRY_TENANT, SETTER_REGISTRY_TENANT } from '../registry/constants';
1314
import { TenantRelationFilter } from './filters/tenant-relation.filter';
1415
import { FilterQuery } from '../registry/interfaces/filter-query';
@@ -49,7 +50,7 @@ export class AppTypeOrmModule {
4950
name: SETTER_REGISTRY_TENANT,
5051
services: {
5152
tenant: TenantRelationSetter<TenantAware>,
52-
user: OwnerRelationSetter<UserAware>,
53+
user: OwnerRelationSetter<UserAware & BaseEntity>,
5354
},
5455
}),
5556
EventEmitter2,

libs/api/core/src/typeorm/setters/owner-relation.setter.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { RequestContextService } from '../../context/app-request-context';
88

99
import { USER_ENTITY } from '../../entity-tokens';
1010
import { EntitySetter } from '../../registry/interfaces/entity-setter';
11+
import BaseEntity from '../../database/entity/base.entity';
1112

1213
@Injectable()
13-
export class OwnerRelationSetter<Entity extends UserAware> implements EntitySetter<Entity> {
14+
export class OwnerRelationSetter<Entity extends UserAware & BaseEntity> implements EntitySetter<Entity> {
1415
constructor(readonly configService: ConfigService) {}
1516

1617
supports(metadata: EntityMetadata): boolean {
@@ -20,9 +21,11 @@ export class OwnerRelationSetter<Entity extends UserAware> implements EntitySett
2021
execute<T extends DeepPartial<Entity>>(entityOrEntities: T | T[]): void {
2122
if (Array.isArray(entityOrEntities)) {
2223
entityOrEntities.forEach((entity) => {
23-
entity.user = { id: RequestContextService.getCurrentUserId() };
24+
if (entity.id === null) {
25+
entity.user = { id: RequestContextService.getCurrentUserId() };
26+
}
2427
});
25-
} else {
28+
} else if (entityOrEntities.id === null || entityOrEntities.id === undefined) {
2629
entityOrEntities.user = { id: RequestContextService.getCurrentUserId() };
2730
}
2831
}

0 commit comments

Comments
 (0)