Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Subscribe user on assignment / Un-assignment #8574

Merged
merged 5 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventBus } from '@nestjs/cqrs';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ILike, In, IsNull, SelectQueryBuilder } from 'typeorm';
import {
Expand All @@ -13,7 +14,8 @@ import {
IOrganizationProjectsFindInput,
IOrganizationProjectUpdateInput,
IPagination,
RolesEnum
RolesEnum,
SubscriptionTypeEnum
} from '@gauzy/contracts';
import { getConfig } from '@gauzy/config';
import { CustomEmbeddedFieldConfig, isNotEmpty } from '@gauzy/common';
Expand All @@ -22,10 +24,12 @@ import { RequestContext } from '../core/context';
import { OrganizationProjectEmployee } from '../core/entities/internal';
import { FavoriteService } from '../core/decorators';
import { RoleService } from '../role/role.service';
import { SubscriptionService } from '../subscription/subscription.service';
import { ActivityLogService } from '../activity-log/activity-log.service';
import { EmployeeService } from '../employee/employee.service';
import { CreateSubscriptionEvent } from '../subscription/events';
import { OrganizationProject } from './organization-project.entity';
import { prepareSQLQuery as p } from './../database/database.helper';
import { EmployeeService } from '../employee/employee.service';
import { TypeOrmEmployeeRepository } from '../employee/repository';
import {
MikroOrmOrganizationProjectEmployeeRepository,
Expand All @@ -38,13 +42,15 @@ import {
@Injectable()
export class OrganizationProjectService extends TenantAwareCrudService<OrganizationProject> {
constructor(
private readonly _eventBus: EventBus,
readonly typeOrmOrganizationProjectRepository: TypeOrmOrganizationProjectRepository,
readonly mikroOrmOrganizationProjectRepository: MikroOrmOrganizationProjectRepository,
readonly typeOrmOrganizationProjectEmployeeRepository: TypeOrmOrganizationProjectEmployeeRepository,
readonly mikroOrmOrganizationProjectEmployeeRepository: MikroOrmOrganizationProjectEmployeeRepository,
readonly typeOrmEmployeeRepository: TypeOrmEmployeeRepository,
private readonly _roleService: RoleService,
private readonly _employeeService: EmployeeService,
private readonly _subscriptionService: SubscriptionService,
private readonly _activityLogService: ActivityLogService
) {
super(typeOrmOrganizationProjectRepository, mikroOrmOrganizationProjectRepository);
Expand Down Expand Up @@ -122,6 +128,27 @@ export class OrganizationProjectService extends TenantAwareCrudService<Organizat
tenantId
});

// Subscribe creator and assignees to the project
try {
await Promise.all(
employees.map(({ id, userId }) =>
this._eventBus.publish(
new CreateSubscriptionEvent({
entity: BaseEntityEnum.OrganizationProject,
entityId: project.id,
userId,
type:
id === employeeId
? SubscriptionTypeEnum.CREATED_ENTITY
: SubscriptionTypeEnum.ASSIGNMENT,
organizationId,
tenantId
})
)
)
);
} catch (error) {}

GloireMutaliko21 marked this conversation as resolved.
Show resolved Hide resolved
// Generate the activity log
this._activityLogService.logActivity<OrganizationProject>(
BaseEntityEnum.OrganizationProject,
Expand Down Expand Up @@ -268,6 +295,21 @@ export class OrganizationProjectService extends TenantAwareCrudService<Organizat
// 1. Remove members who are no longer assigned to the project
if (removedMembers.length) {
await this.deleteMemberByIds(removedMembers.map((member) => member.id));

// Unsubscribe members who were unassigned from project
try {
await Promise.all(
removedMembers.map(
async (member) =>
await this._subscriptionService.delete({
entity: BaseEntityEnum.OrganizationProject,
entityId: organizationProjectId,
userId: member.employee.userId,
type: SubscriptionTypeEnum.ASSIGNMENT
})
)
);
} catch (error) {}
GloireMutaliko21 marked this conversation as resolved.
Show resolved Hide resolved
}

// 2. Update roles for existing members where necessary
Expand Down Expand Up @@ -300,6 +342,24 @@ export class OrganizationProjectService extends TenantAwareCrudService<Organizat
})
);

// Subscribe new assignees to the project
try {
await Promise.all(
newMembers.map(({ userId }) =>
this._eventBus.publish(
new CreateSubscriptionEvent({
entity: BaseEntityEnum.OrganizationProject,
entityId: organizationProjectId,
userId,
type: SubscriptionTypeEnum.ASSIGNMENT,
organizationId,
tenantId
})
)
)
);
} catch (error) {}

GloireMutaliko21 marked this conversation as resolved.
Show resolved Hide resolved
await this.typeOrmOrganizationProjectEmployeeRepository.save(newProjectMembers);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EventBus } from '@nestjs/cqrs';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import {
BaseEntityEnum,
Expand All @@ -8,16 +9,19 @@ import {
IOrganizationSprintCreateInput,
IOrganizationSprintUpdateInput,
RolesEnum,
ActionTypeEnum
ActionTypeEnum,
SubscriptionTypeEnum
} from '@gauzy/contracts';
import { isNotEmpty } from '@gauzy/common';
import { TenantAwareCrudService } from './../core/crud';
import { RequestContext } from '../core/context';
import { OrganizationSprintEmployee } from '../core/entities/internal';
import { FavoriteService } from '../core/decorators';
// import { prepareSQLQuery as p } from './../database/database.helper';
import { CreateSubscriptionEvent } from '../subscription/events';
import { RoleService } from '../role/role.service';
import { EmployeeService } from '../employee/employee.service';
import { SubscriptionService } from '../subscription/subscription.service';
import { ActivityLogService } from '../activity-log/activity-log.service';
import { OrganizationSprint } from './organization-sprint.entity';
import { TypeOrmEmployeeRepository } from '../employee/repository';
Expand All @@ -32,13 +36,15 @@ import {
@Injectable()
export class OrganizationSprintService extends TenantAwareCrudService<OrganizationSprint> {
constructor(
private readonly _eventBus: EventBus,
readonly typeOrmOrganizationSprintRepository: TypeOrmOrganizationSprintRepository,
readonly mikroOrmOrganizationSprintRepository: MikroOrmOrganizationSprintRepository,
readonly typeOrmOrganizationSprintEmployeeRepository: TypeOrmOrganizationSprintEmployeeRepository,
readonly mikroOrmOrganizationSprintEmployeeRepository: MikroOrmOrganizationSprintEmployeeRepository,
readonly typeOrmEmployeeRepository: TypeOrmEmployeeRepository,
private readonly _roleService: RoleService,
private readonly _employeeService: EmployeeService,
private readonly subscriptionService: SubscriptionService,
private readonly activityLogService: ActivityLogService
) {
super(typeOrmOrganizationSprintRepository, mikroOrmOrganizationSprintRepository);
Expand Down Expand Up @@ -113,6 +119,27 @@ export class OrganizationSprintService extends TenantAwareCrudService<Organizati
tenantId
});

// Subscribe creator and assignees to the sprint
try {
await Promise.all(
employees.map(({ id, userId }) =>
this._eventBus.publish(
new CreateSubscriptionEvent({
entity: BaseEntityEnum.OrganizationSprint,
entityId: sprint.id,
userId,
type:
id === employeeId
? SubscriptionTypeEnum.CREATED_ENTITY
: SubscriptionTypeEnum.ASSIGNMENT,
organizationId,
tenantId
})
)
)
);
} catch (error) {}

GloireMutaliko21 marked this conversation as resolved.
Show resolved Hide resolved
// Generate the activity log
this.activityLogService.logActivity<OrganizationSprint>(
BaseEntityEnum.OrganizationSprint,
Expand Down Expand Up @@ -263,6 +290,21 @@ export class OrganizationSprintService extends TenantAwareCrudService<Organizati
// 1. Remove members who are no longer assigned to the sprint
if (removedMembers.length) {
await this.deleteMemberByIds(removedMembers.map((member) => member.id));

// Unsubscribe members who were unassigned from sprint
try {
await Promise.all(
removedMembers.map(
async (member) =>
await this.subscriptionService.delete({
entity: BaseEntityEnum.OrganizationSprint,
entityId: organizationSprintId,
userId: member.employee.userId,
type: SubscriptionTypeEnum.ASSIGNMENT
})
)
);
} catch (error) {}
GloireMutaliko21 marked this conversation as resolved.
Show resolved Hide resolved
}

// 2. Update roles for existing members where necessary.
Expand Down Expand Up @@ -292,6 +334,24 @@ export class OrganizationSprintService extends TenantAwareCrudService<Organizati
})
);

// Subscribe new assignees to the sprint
try {
await Promise.all(
newMembers.map(({ userId }) =>
this._eventBus.publish(
new CreateSubscriptionEvent({
entity: BaseEntityEnum.OrganizationSprint,
entityId: organizationSprintId,
userId,
type: SubscriptionTypeEnum.ASSIGNMENT,
organizationId,
tenantId
})
)
)
);
} catch (error) {}

GloireMutaliko21 marked this conversation as resolved.
Show resolved Hide resolved
await this.typeOrmOrganizationSprintEmployeeRepository.save(newSprintMembers);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CqrsModule } from '@nestjs/cqrs';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { MikroOrmModule } from '@mikro-orm/nestjs';
Expand All @@ -13,6 +14,7 @@ import { TypeOrmOrganizationTeamEmployeeRepository } from './repository';
TypeOrmModule.forFeature([OrganizationTeamEmployee]),
MikroOrmModule.forFeature([OrganizationTeamEmployee]),
RolePermissionModule,
CqrsModule,
TaskModule
],
controllers: [OrganizationTeamEmployeeController],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { EventBus } from '@nestjs/cqrs';
import { ForbiddenException, Injectable } from '@nestjs/common';
import { DeleteResult, FindOptionsWhere, UpdateResult } from 'typeorm';
import {
BaseEntityEnum,
ID,
IEmployee,
IOrganizationTeamEmployeeActiveTaskUpdateInput,
IOrganizationTeamEmployeeFindInput,
IOrganizationTeamEmployeeUpdateInput,
PermissionsEnum,
RolesEnum
RolesEnum,
SubscriptionTypeEnum
} from '@gauzy/contracts';
import { TenantAwareCrudService } from './../core/crud';
import { RequestContext } from './../core/context';
import { Role } from './../core/entities/internal';
import { OrganizationTeamEmployee } from './organization-team-employee.entity';
import { TaskService } from './../tasks/task.service';
import { CreateSubscriptionEvent } from '../subscription/events';
import { SubscriptionService } from '../subscription/subscription.service';
import { MikroOrmOrganizationTeamEmployeeRepository, TypeOrmOrganizationTeamEmployeeRepository } from './repository';

@Injectable()
export class OrganizationTeamEmployeeService extends TenantAwareCrudService<OrganizationTeamEmployee> {
constructor(
private readonly _eventBus: EventBus,
readonly typeOrmOrganizationTeamEmployeeRepository: TypeOrmOrganizationTeamEmployeeRepository,
readonly mikroOrmOrganizationTeamEmployeeRepository: MikroOrmOrganizationTeamEmployeeRepository,
private readonly taskService: TaskService
private readonly taskService: TaskService,
private readonly subscriptionService: SubscriptionService
) {
super(typeOrmOrganizationTeamEmployeeRepository, mikroOrmOrganizationTeamEmployeeRepository);
}
Expand Down Expand Up @@ -64,6 +71,21 @@ export class OrganizationTeamEmployeeService extends TenantAwareCrudService<Orga
// 1. Remove members who are no longer in the team
if (removedMembers.length > 0) {
await this.deleteMemberByIds(removedMembers.map((member) => member.id));

// Unsubscribe members who were unassigned from team
try {
await Promise.all(
removedMembers.map(
async (member) =>
await this.subscriptionService.delete({
entity: BaseEntityEnum.OrganizationTeam,
entityId: organizationTeamId,
userId: member.employee.userId,
type: SubscriptionTypeEnum.ASSIGNMENT
})
)
);
} catch (error) {}
GloireMutaliko21 marked this conversation as resolved.
Show resolved Hide resolved
}

// 2. Update role for existing members
Expand Down Expand Up @@ -97,6 +119,24 @@ export class OrganizationTeamEmployeeService extends TenantAwareCrudService<Orga
})
);

// Subscribe new assignees to the team
try {
await Promise.all(
newMembers.map(({ userId }) =>
this._eventBus.publish(
new CreateSubscriptionEvent({
entity: BaseEntityEnum.OrganizationTeam,
entityId: organizationTeamId,
userId,
type: SubscriptionTypeEnum.ASSIGNMENT,
organizationId,
tenantId
})
)
)
);
} catch (error) {}

GloireMutaliko21 marked this conversation as resolved.
Show resolved Hide resolved
await this.typeOrmRepository.save(newTeamMembers);
}
}
Expand Down
Loading
Loading