Skip to content

Commit

Permalink
Merge pull request #7058 from ever-co/fix/integration-task-sync-dupli…
Browse files Browse the repository at this point in the history
…cate

[Fix] Integration Sync Duplicate Task
  • Loading branch information
evereq authored Oct 31, 2023
2 parents 7512651 + 71d3211 commit 87eff57
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ import { DateViewComponent } from '../../@shared/table-components';
})
export class EquipmentSharingComponent
extends PaginationFilterBaseComponent
implements OnInit, OnDestroy
{
implements OnInit, OnDestroy {
loading: boolean = false;
disableButton: boolean = true;

Expand Down Expand Up @@ -110,8 +109,8 @@ export class EquipmentSharingComponent
this.selectedEmployeeId = user.employee
? user.employee.id
: employee
? employee.id
: null;
? employee.id
: null;
}),
tap(() => this._refresh$.next(true)),
tap(() => this.equipmentSharing$.next(true)),
Expand Down Expand Up @@ -141,7 +140,7 @@ export class EquipmentSharingComponent
.subscribe();
}

ngOnDestroy() {}
ngOnDestroy() { }

setView() {
this.viewComponentName = ComponentEnum.EQUIPMENT_SHARING;
Expand All @@ -158,6 +157,7 @@ export class EquipmentSharingComponent
(componentLayout) =>
componentLayout === ComponentLayoutStyleEnum.CARDS_GRID
),
tap(() => this.equipments = []),
tap(() => this.equipmentSharing$.next(true)),
untilDestroyed(this)
)
Expand Down Expand Up @@ -246,51 +246,51 @@ export class EquipmentSharingComponent
/**
* Show/Hide Equipment Sharing Approved Button
*
* @param equipement
* @param equipment
* @returns
*/
showApprovedButton(equipementSharing: IEquipmentSharing) {
if (equipementSharing) {
showApprovedButton(equipmentSharing: IEquipmentSharing) {
if (equipmentSharing) {
const statues: RequestApprovalStatusTypesEnum[] = [
RequestApprovalStatusTypesEnum.REFUSED,
RequestApprovalStatusTypesEnum.REQUESTED
];
return statues.includes(equipementSharing.status);
return statues.includes(equipmentSharing.status);
}
return false;
}

/**
* Show/Hide Equipment Sharing Refuse Button
*
* @param equipement
* @param equipment
* @returns
*/
showRefuseButton(equipementSharing: IEquipmentSharing) {
if (equipementSharing) {
showRefuseButton(equipmentSharing: IEquipmentSharing) {
if (equipmentSharing) {
const statues: RequestApprovalStatusTypesEnum[] = [
RequestApprovalStatusTypesEnum.APPROVED,
RequestApprovalStatusTypesEnum.REQUESTED
];
return statues.includes(equipementSharing.status);
return statues.includes(equipmentSharing.status);
}
return false;
}

async approval(equipementSharing: IEquipmentSharing) {
async approval(equipmentSharing: IEquipmentSharing) {
if (!this.organization) {
return;
}
try {
if (this.showApprovedButton(equipementSharing)) {
if (this.showApprovedButton(equipmentSharing)) {
const request = await this.equipmentSharingService.approval(
equipementSharing.id
equipmentSharing.id
);
if (request) {
this.toastrService.success(
'EQUIPMENT_SHARING_PAGE.APPROVAL_SUCCESS',
{
name: equipementSharing.name
name: equipmentSharing.name
}
);
}
Expand All @@ -302,20 +302,20 @@ export class EquipmentSharingComponent
}
}

async refuse(equipementSharing: IEquipmentSharing) {
async refuse(equipmentSharing: IEquipmentSharing) {
if (!this.organization) {
return;
}
try {
if (this.showRefuseButton(equipementSharing)) {
if (this.showRefuseButton(equipmentSharing)) {
const request = await this.equipmentSharingService.refuse(
equipementSharing.id
equipmentSharing.id
);
if (request) {
this.toastrService.success(
'EQUIPMENT_SHARING_PAGE.APPROVAL_SUCCESS',
{
name: equipementSharing.name
name: equipmentSharing.name
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/equipment/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { CreateEquipmentDTO } from "./create-equipment.dto"
export { UpdateEquipmentDTO } from "./update-equipement.dto"
export { UpdateEquipmentDTO } from "./update-equipment.dto"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CommandHandler, ICommandHandler, CommandBus } from '@nestjs/cqrs';
import { IntegrationEntity } from '@gauzy/contracts';
import { TaskService } from 'tasks/task.service';
import { IntegrationMapSyncEntityCommand } from './../integration-map.sync-entity.command';
import { IntegrationMapSyncTaskCommand } from './../integration-map.sync-task.command';
import { IntegrationMapService } from '../../integration-map.service';
Expand All @@ -12,7 +13,8 @@ export class IntegrationMapSyncTaskHandler

constructor(
private readonly _commandBus: CommandBus,
private readonly _integrationMapService: IntegrationMapService
private readonly _integrationMapService: IntegrationMapService,
private readonly _taskService: TaskService
) { }

/**
Expand All @@ -24,33 +26,56 @@ export class IntegrationMapSyncTaskHandler
public async execute(
command: IntegrationMapSyncTaskCommand
) {
const { input } = command;
const { triggeredEvent, input } = command;
const { sourceId, organizationId, integrationId, entity } = input;
const tenantId = RequestContext.currentTenantId() || input.tenantId;

try {
const taskMap = await this._integrationMapService.findOneByWhereOptions({
// Check if an integration map already exists for the issue
const integrationMap = await this._integrationMapService.findOneByWhereOptions({
entity: IntegrationEntity.TASK,
sourceId,
integrationId,
organizationId,
tenantId
});
await this._commandBus.execute(
new TaskUpdateCommand(taskMap.gauzyId, entity)
);
return taskMap;

// Try to find the corresponding task
try {
await this._taskService.findOneByIdString(integrationMap.gauzyId);

// Update the corresponding task with the new input data
await this._commandBus.execute(
new TaskUpdateCommand(integrationMap.gauzyId, entity, triggeredEvent)
);
} catch (error) {
console.log(`${IntegrationEntity.TASK} Not Found for integration GauzyID %s: `, integrationMap.gauzyId);
// Create a corresponding task with the new input data
await this._commandBus.execute(
new TaskCreateCommand({
...entity,
id: integrationMap.gauzyId
})
);
}
// Return the integration map
return integrationMap;
} catch (error) {
// Handle errors and create a new task
// Create a new task with the provided entity data
const task = await this._commandBus.execute(
new TaskCreateCommand(entity)
new TaskCreateCommand(entity, triggeredEvent)
);

// Create a new integration map for the issue
return await this._commandBus.execute(
new IntegrationMapSyncEntityCommand({
gauzyId: task.id,
entity: IntegrationEntity.TASK,
integrationId,
sourceId,
entity: IntegrationEntity.TASK,
organizationId
organizationId,
tenantId
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class IntegrationMapSyncTaskCommand implements ICommand {
static readonly type = '[Integration Map] Sync Task';

constructor(
public readonly input: IIntegrationMapSyncEntity<ITaskCreateInput | ITaskUpdateInput>
public readonly input: IIntegrationMapSyncEntity<ITaskCreateInput | ITaskUpdateInput>,
public readonly triggeredEvent: boolean = true // Enabled the "2 Way Sync Triggered Event" Synchronization
) { }
}
5 changes: 4 additions & 1 deletion packages/core/src/integration/hubstaff/hubstaff.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ export class HubstaffService {
if (!due_at) {
due_at = new Date(moment().add(2, 'week').format('YYYY-MM-DD HH:mm:ss'));
}

// Step 1: Execute a command to initiate the synchronization process
const triggeredEvent = false;
return await this._commandBus.execute(
new IntegrationMapSyncTaskCommand({
entity: {
Expand All @@ -520,7 +523,7 @@ export class HubstaffService {
integrationId,
organizationId,
tenantId
})
}, triggeredEvent)
);
}
)
Expand Down

0 comments on commit 87eff57

Please sign in to comment.