mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
add server to monorepo.
This commit is contained in:
71
packages/server/src/services/Users/SyncTenantUserSaved.ts
Normal file
71
packages/server/src/services/Users/SyncTenantUserSaved.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { pick } from 'lodash';
|
||||
import {
|
||||
ITenantUser,
|
||||
ITenantUserActivatedPayload,
|
||||
ITenantUserDeletedPayload,
|
||||
ITenantUserEditedPayload,
|
||||
ITenantUserInactivatedPayload,
|
||||
} from '@/interfaces';
|
||||
import events from '@/subscribers/events';
|
||||
import { SystemUser } from '@/system/models';
|
||||
|
||||
export default class SyncTenantUserMutate {
|
||||
/**
|
||||
* Attaches events with handlers.
|
||||
* @param bus
|
||||
*/
|
||||
attach(bus) {
|
||||
bus.subscribe(events.tenantUser.onEdited, this.syncSystemUserOnceEdited);
|
||||
bus.subscribe(
|
||||
events.tenantUser.onActivated,
|
||||
this.syncSystemUserOnceActivated
|
||||
);
|
||||
bus.subscribe(
|
||||
events.tenantUser.onInactivated,
|
||||
this.syncSystemUserOnceInactivated
|
||||
);
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param tenantUser
|
||||
*/
|
||||
private syncSystemUserOnceEdited = async ({
|
||||
tenantUser,
|
||||
}: ITenantUserEditedPayload) => {
|
||||
await SystemUser.query()
|
||||
.where('id', tenantUser.systemUserId)
|
||||
.patch({
|
||||
...pick(tenantUser, [
|
||||
'firstName',
|
||||
'lastName',
|
||||
'email',
|
||||
'active',
|
||||
'phoneNumber',
|
||||
]),
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Syncs activate system user.
|
||||
* @param {ITenantUserInactivatedPayload} payload -
|
||||
*/
|
||||
private syncSystemUserOnceActivated = async ({
|
||||
tenantUser,
|
||||
}: ITenantUserInactivatedPayload) => {
|
||||
await SystemUser.query().where('id', tenantUser.systemUserId).patch({
|
||||
active: true,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Syncs inactivate system user.
|
||||
* @param {ITenantUserActivatedPayload} payload -
|
||||
*/
|
||||
private syncSystemUserOnceInactivated = async ({
|
||||
tenantUser,
|
||||
}: ITenantUserActivatedPayload) => {
|
||||
await SystemUser.query().where('id', tenantUser.systemUserId).patch({
|
||||
active: false,
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user