fix(SPG-644) Sync ketika privileges di update
parent
d81eaac4f6
commit
20848aab3c
|
@ -1,5 +1,6 @@
|
|||
import { validateRelations } from 'src/core/strings/constants/interface.constants';
|
||||
import { BaseManager } from '../base.manager';
|
||||
import { OPERATION } from 'src/core/strings/constants/base.constants';
|
||||
|
||||
export abstract class BaseCustomManager<Entity> extends BaseManager {
|
||||
protected result: any;
|
||||
|
@ -23,4 +24,31 @@ export abstract class BaseCustomManager<Entity> extends BaseManager {
|
|||
}
|
||||
|
||||
abstract getResult(): any;
|
||||
|
||||
async publishEvents() {
|
||||
if (!this.eventTopics.length) return;
|
||||
for (const topic of this.eventTopics) {
|
||||
let data;
|
||||
if (!topic.data) {
|
||||
data = await this.dataService.getOneByOptions({
|
||||
where: {
|
||||
id: this.result['id'],
|
||||
},
|
||||
relations: topic.relations,
|
||||
});
|
||||
}
|
||||
|
||||
this.eventBus.publishAll([
|
||||
new topic.topic({
|
||||
id: data?.['id'] ?? topic?.data?.['id'],
|
||||
old: null,
|
||||
data: data ?? topic.data,
|
||||
user: this.user,
|
||||
description: '',
|
||||
module: this.tableName,
|
||||
op: OPERATION.UPDATE,
|
||||
}),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import {
|
|||
} from './domain/managers/item.handler';
|
||||
import {
|
||||
UserDeletedHandler,
|
||||
UserPrivilegeUpdateHandler,
|
||||
UserUpdatedHandler,
|
||||
} from './domain/managers/user.handler';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
@ -69,6 +70,7 @@ import { VipCodeCreatedHandler } from './domain/managers/vip-code.handler';
|
|||
ItemDeletedHandler,
|
||||
UserDeletedHandler,
|
||||
UserUpdatedHandler,
|
||||
UserPrivilegeUpdateHandler,
|
||||
|
||||
TransactionDataService,
|
||||
UserDataService,
|
||||
|
|
|
@ -62,7 +62,6 @@ export class CouchService {
|
|||
const nano = this.nanoInstance;
|
||||
const db = nano.use(database);
|
||||
const result = await db.get(data.id);
|
||||
console.log(result, 'dsa');
|
||||
await db.insert({
|
||||
...data,
|
||||
_rev: result._rev,
|
||||
|
|
|
@ -5,6 +5,7 @@ import { UserDeletedEvent } from 'src/modules/user-related/user/domain/entities/
|
|||
import { UserChangeStatusEvent } from 'src/modules/user-related/user/domain/entities/event/user-change-status.event';
|
||||
import { UserUpdatedEvent } from 'src/modules/user-related/user/domain/entities/event/user-updated.event';
|
||||
import { UserDataService } from 'src/modules/user-related/user/data/services/user-data.service';
|
||||
import { UserPrivilegeConfigUpdatedEvent } from 'src/modules/user-related/user-privilege/domain/entities/event/user-privilege-configuration-updated.event';
|
||||
|
||||
@EventsHandler(UserDeletedEvent)
|
||||
export class UserDeletedHandler implements IEventHandler<UserDeletedEvent> {
|
||||
|
@ -86,3 +87,53 @@ export class UserUpdatedHandler
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventsHandler(UserPrivilegeConfigUpdatedEvent)
|
||||
export class UserPrivilegeUpdateHandler
|
||||
implements IEventHandler<UserPrivilegeConfigUpdatedEvent>
|
||||
{
|
||||
constructor(
|
||||
private couchService: CouchService,
|
||||
private userService: UserDataService,
|
||||
) {}
|
||||
|
||||
async handle(event: UserPrivilegeConfigUpdatedEvent) {
|
||||
const data = event.data.data;
|
||||
|
||||
const users = await this.userService
|
||||
.getManyByOptions({
|
||||
where: {
|
||||
user_privilege_id: data.user_privilege_id,
|
||||
status: STATUS.ACTIVE,
|
||||
},
|
||||
relations: [
|
||||
'user_privilege',
|
||||
'user_privilege.user_privilege_configurations',
|
||||
],
|
||||
})
|
||||
.then((items) => {
|
||||
return items?.map((item) => {
|
||||
console.log(item, 'dsa');
|
||||
const user_privilege_configurations = item[
|
||||
'user_privilege'
|
||||
]?.user_privilege_configurations?.filter(
|
||||
(config) => config.module == 'POS',
|
||||
);
|
||||
Object.assign(item['user_privilege'], {
|
||||
user_privilege_configurations: user_privilege_configurations,
|
||||
});
|
||||
return item;
|
||||
});
|
||||
});
|
||||
|
||||
for (const user of users) {
|
||||
await this.couchService.updateDoc(
|
||||
{
|
||||
_id: user.id,
|
||||
...user,
|
||||
},
|
||||
'user',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
import { IEvent } from 'src/core/strings/constants/interface.constants';
|
||||
|
||||
export class UserPrivilegeConfigUpdatedEvent {
|
||||
constructor(public readonly data: IEvent) {}
|
||||
}
|
|
@ -6,6 +6,7 @@ import {
|
|||
} from 'src/core/strings/constants/interface.constants';
|
||||
import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base-custom.manager';
|
||||
import { UserPrivilegeConfigurationModel } from 'src/modules/user-related/user-privilege/data/models/user-privilege-configuration.model';
|
||||
import { UserPrivilegeConfigUpdatedEvent } from '../../../entities/event/user-privilege-configuration-updated.event';
|
||||
|
||||
@Injectable()
|
||||
export class UpdateUserPrivilegeConfigurationManager extends BaseCustomManager<UserPrivilegeConfigurationEntity> {
|
||||
|
@ -24,6 +25,8 @@ export class UpdateUserPrivilegeConfigurationManager extends BaseCustomManager<U
|
|||
{ id: this.data.id },
|
||||
this.data,
|
||||
);
|
||||
|
||||
this.publishEvents();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -44,6 +47,11 @@ export class UpdateUserPrivilegeConfigurationManager extends BaseCustomManager<U
|
|||
}
|
||||
|
||||
get eventTopics(): EventTopics[] {
|
||||
return [];
|
||||
return [
|
||||
{
|
||||
topic: UserPrivilegeConfigUpdatedEvent,
|
||||
data: this.data,
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue