mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
fix: retrieve the build org job state
This commit is contained in:
@@ -2,7 +2,6 @@ import { Inject, Injectable } from '@nestjs/common';
|
||||
import { ERRORS } from '../Bills.constants';
|
||||
import { Bill } from '../models/Bill';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
import { IItemEntryDTO } from '@/modules/TransactionItemEntry/ItemEntry.types';
|
||||
import { Item } from '@/modules/Items/models/Item';
|
||||
import { BillPaymentEntry } from '@/modules/BillPayments/models/BillPaymentEntry';
|
||||
import { BillLandedCost } from '@/modules/BillLandedCosts/models/BillLandedCost';
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
ApiResponse,
|
||||
ApiBody,
|
||||
ApiOkResponse,
|
||||
} from '@nestjs/swagger';
|
||||
import {
|
||||
Controller,
|
||||
Post,
|
||||
@@ -9,6 +15,7 @@ import {
|
||||
Res,
|
||||
Next,
|
||||
HttpCode,
|
||||
Param,
|
||||
} from '@nestjs/common';
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { BuildOrganizationService } from './commands/BuildOrganization.service';
|
||||
@@ -20,6 +27,7 @@ import { GetCurrentOrganizationService } from './queries/GetCurrentOrganization.
|
||||
import { UpdateOrganizationService } from './commands/UpdateOrganization.service';
|
||||
import { IgnoreTenantInitializedRoute } from '../Tenancy/EnsureTenantIsInitialized.guard';
|
||||
import { IgnoreTenantSeededRoute } from '../Tenancy/EnsureTenantIsSeeded.guards';
|
||||
import { GetBuildOrganizationBuildJob } from './commands/GetBuildOrganizationJob.service';
|
||||
|
||||
@ApiTags('Organization')
|
||||
@Controller('organization')
|
||||
@@ -30,6 +38,7 @@ export class OrganizationController {
|
||||
private readonly buildOrganizationService: BuildOrganizationService,
|
||||
private readonly getCurrentOrgService: GetCurrentOrganizationService,
|
||||
private readonly updateOrganizationService: UpdateOrganizationService,
|
||||
private readonly getBuildOrganizationJobService: GetBuildOrganizationBuildJob,
|
||||
) {}
|
||||
|
||||
@Post('build')
|
||||
@@ -51,6 +60,13 @@ export class OrganizationController {
|
||||
};
|
||||
}
|
||||
|
||||
@Get('build/:buildJobId')
|
||||
@HttpCode(200)
|
||||
@ApiOperation({ summary: 'Gets the organization build job details' })
|
||||
async buildJob(@Param('buildJobId') buildJobId: string) {
|
||||
return this.getBuildOrganizationJobService.getJobDetails(buildJobId);
|
||||
}
|
||||
|
||||
@Get('current')
|
||||
@HttpCode(200)
|
||||
@ApiOperation({ summary: 'Get current organization' })
|
||||
|
||||
@@ -12,6 +12,7 @@ import { TenantDBManagerModule } from '../TenantDBManager/TenantDBManager.module
|
||||
import { OrganizationBaseCurrencyLocking } from './Organization/OrganizationBaseCurrencyLocking.service';
|
||||
import { SyncSystemUserToTenantService } from './commands/SyncSystemUserToTenant.service';
|
||||
import { SyncSystemUserToTenantSubscriber } from './subscribers/SyncSystemUserToTenant.subscriber';
|
||||
import { GetBuildOrganizationBuildJob } from './commands/GetBuildOrganizationJob.service';
|
||||
|
||||
@Module({
|
||||
providers: [
|
||||
@@ -23,7 +24,8 @@ import { SyncSystemUserToTenantSubscriber } from './subscribers/SyncSystemUserTo
|
||||
CommandOrganizationValidators,
|
||||
OrganizationBaseCurrencyLocking,
|
||||
SyncSystemUserToTenantService,
|
||||
SyncSystemUserToTenantSubscriber
|
||||
SyncSystemUserToTenantSubscriber,
|
||||
GetBuildOrganizationBuildJob
|
||||
],
|
||||
imports: [
|
||||
BullModule.registerQueue({ name: OrganizationBuildQueue }),
|
||||
|
||||
@@ -58,3 +58,9 @@ export const OrganizationBuildQueueJob = 'OrganizationBuildQueueJob';
|
||||
export interface OrganizationBuildQueueJobPayload extends TenantJobPayload {
|
||||
buildDto: BuildOrganizationDto;
|
||||
}
|
||||
|
||||
export interface BuildOrganizationResult {
|
||||
delay: number;
|
||||
processedOn: number;
|
||||
jobId: string;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import { Queue } from 'bullmq';
|
||||
import { InjectQueue } from '@nestjs/bullmq';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
BuildOrganizationResult,
|
||||
IOrganizationBuildEventPayload,
|
||||
IOrganizationBuiltEventPayload,
|
||||
OrganizationBuildQueue,
|
||||
OrganizationBuildQueueJob,
|
||||
OrganizationBuildQueueJobPayload,
|
||||
} from '../Organization.types';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
|
||||
import {
|
||||
throwIfTenantInitizalized,
|
||||
@@ -48,7 +49,7 @@ export class BuildOrganizationService {
|
||||
await this.tenantsManager.createDatabase();
|
||||
|
||||
await this.tenantsManager.migrateTenant();
|
||||
await this.tenantsManager.seedTenant()
|
||||
await this.tenantsManager.seedTenant();
|
||||
|
||||
// Throws `onOrganizationBuild` event.
|
||||
await this.eventPublisher.emitAsync(events.organization.build, {
|
||||
@@ -77,7 +78,7 @@ export class BuildOrganizationService {
|
||||
*/
|
||||
async buildRunJob(
|
||||
buildDTO: BuildOrganizationDto,
|
||||
): Promise<{ nextRunAt: Date; jobId: string }> {
|
||||
): Promise<BuildOrganizationResult> {
|
||||
const tenant = await this.tenancyContext.getTenant();
|
||||
const systemUser = await this.tenancyContext.getSystemUser();
|
||||
|
||||
@@ -106,8 +107,9 @@ export class BuildOrganizationService {
|
||||
await this.tenantRepository.markAsBuilding(jobMeta.id).findById(tenant.id);
|
||||
|
||||
return {
|
||||
nextRunAt: jobMeta.data.nextRunAt,
|
||||
jobId: jobMeta.data.id,
|
||||
delay: jobMeta.delay,
|
||||
processedOn: jobMeta.processedOn,
|
||||
jobId: jobMeta.id,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Queue } from 'bullmq';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectQueue } from '@nestjs/bullmq';
|
||||
import { OrganizationBuildQueue } from '../Organization.types';
|
||||
import { ServiceError } from '@/modules/Items/ServiceError';
|
||||
|
||||
@Injectable()
|
||||
export class GetBuildOrganizationBuildJob {
|
||||
constructor(
|
||||
@InjectQueue(OrganizationBuildQueue)
|
||||
private readonly organizationBuildQueue: Queue,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Gets the build job details by job ID.
|
||||
* @param {string} jobId - The ID of the job to retrieve.
|
||||
* @returns {Promise<any>} - Returns the job details.
|
||||
*/
|
||||
async getJobDetails(jobId: string): Promise<any> {
|
||||
const job = await this.organizationBuildQueue.getJob(jobId);
|
||||
|
||||
if (!job) {
|
||||
throw new ServiceError('Job not found', 'JOB.NOT_FOUND');
|
||||
}
|
||||
const state = await job.getState();
|
||||
|
||||
return {
|
||||
id: job.id,
|
||||
state,
|
||||
progress: job.progress,
|
||||
isCompleted: state === 'completed',
|
||||
isRunning: state === 'active',
|
||||
isWaiting: state === 'waiting' || state === 'waiting-children',
|
||||
isFailed: state === 'failed',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user