Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
189 changes: 189 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { Body, Controller, Get, Logger, Param, Post, Query, UseGuards } from '@nestjs/common';
import { ThirdPartyApiClientService } from '@/common/third-party-api-client.service';
import { ApifyClientService } from '@/common/apify-client.service';
import { DevelopmentOnlyGuard } from '@/common/guards/development-only.guard';
import { APIFY_JOB_STATUSES } from '@/constants/registration.constants';
import { ApifyPromptPilotDatasetItem } from '@/types/apify.types';
import { CreateCompanyContactDto, CreateCompanyContactResponse } from '@/modules/registration/dto/create-company-contact.dto';
import { JobStatusService } from '@/modules/registration/job-status.service';
import { RegistrationService } from '@/modules/registration/registration.service';
import { IataRegistrationCheckResponse } from '@/types/registration-job-status.types';
import { CheckTipaltiCompletionStatusResponse } from '@/types/tipalti-status-check.types';

@Controller('test')
export class TestController {
private readonly logger = new Logger(TestController.name);

constructor(
private readonly thirdPartyApiClient: ThirdPartyApiClientService,
private readonly apifyClientService: ApifyClientService,
private readonly jobStatusService: JobStatusService,
private readonly registrationService: RegistrationService,
) {}

/**
* Search Ventrata resellers by IATA code
* Only available in development environment
* @param iataCode - The IATA code to search for
*/
@UseGuards(DevelopmentOnlyGuard)
@Get('ventrata/resellers/search')
async searchVentrataResellers(@Query('iataCode') iataCode: string) {
if (!iataCode) {
return {
success: false,
message: 'iataCode query parameter is required',
};
}

try {
this.logger.log(`API call: Searching Ventrata resellers for IATA code: ${iataCode}`);

const result = await this.thirdPartyApiClient.searchVentrataResellers(iataCode);

return {
success: result.success,
data: result.data,
exists: result.exists,
message: result.success ? `Found ${result.data.length} resellers for IATA code ${iataCode}` : result.error || 'Search failed',
};
} catch (error) {
this.logger.error(`API call failed for IATA code ${iataCode}:`, error);
return {
success: false,
message: `Search failed: ${error.message}`,
};
}
}

/**
* Trigger Ventrata registration step (dev-only)
*/
@UseGuards(DevelopmentOnlyGuard)
@Post('ventrata/registration/:registrationId?')
async executeVentrataRegistration(@Body() request: CreateCompanyContactDto, @Param('registrationId') registrationId?: string) {
const registrationIdNumber = registrationId ? Number(registrationId) : undefined;
return this.thirdPartyApiClient.executeVentrataRegistration(request, registrationIdNumber);
}

/**
* Get Apify run status by run ID
* Only available in development environment
* @param runId - The Apify run ID to check status for
*/
@UseGuards(DevelopmentOnlyGuard)
@Get('apify/run-status')
async getApifyRunStatus(@Query('runId') runId: string) {
if (!runId) {
return {
success: false,
message: 'runId query parameter is required',
};
}

try {
this.logger.log(`API call: Getting Apify run status for runId: ${runId}`);

const runResult = await this.apifyClientService.getRunStatus(runId);
const apifyStatus = runResult.status;

if (apifyStatus === APIFY_JOB_STATUSES.SUCCEEDED) {
if (runResult.defaultDatasetId) {
const datasetItems = await this.apifyClientService.getDatasetItems<ApifyPromptPilotDatasetItem>(runResult.defaultDatasetId, 1);
let result = true;
if (datasetItems.length === 0) {
result = false;
this.logger.warn(`No dataset items found for succeeded run ${runId}, treating as failed`);
}

// If error exists and error doesn't contain 'panelAgencyResult2Columns', we treat it as failed
if (datasetItems[0] && datasetItems[0].error && !datasetItems[0].error?.includes('panelAgencyResult2Columns')) {
result = false;
this.logger.warn(`Dataset item contains error without 'panelAgencyResult2Columns' for jobId ${runId}, treating as failed`);
}
return {
success: result,
data: datasetItems,
message: `Retrieved run status: ${runResult.status} for runId ${runId}`,
};
}
}

return {
success: false,
data: null,
message: `Retrieved run status: ${runResult.status} for runId ${runId}`,
};
} catch (error) {
this.logger.error(`API call failed for runId ${runId}:`, error);
return {
success: false,
message: `Failed to get run status: ${error.message}`,
};
}
}

/**
* Check job statuses for registrations with READY job status and PENDING registration status
* Only available in development environment
* POST /test/registration/job-status/check
*/
@UseGuards(DevelopmentOnlyGuard)
@Post('registration/job-status/check')
async checkJobStatuses(): Promise<IataRegistrationCheckResponse> {
try {
this.logger.log('Checking job statuses via API');

const results = await this.jobStatusService.checkJobStatuses();

return {
success: true,
data: results,
message: `Checked ${results.length} job statuses`,
};
} catch (error) {
this.logger.error('Error checking job statuses via API:', error);
return {
success: false,
data: [],
message: `Failed to check job statuses: ${error instanceof Error ? error.message : String(error)}`,
};
}
}

/**
* Register agent and agency in HubSpot (second step of registration form)
* Only available in development environment
* POST /test/registration/workflow
*/
@UseGuards(DevelopmentOnlyGuard)
@Post('registration/workflow')
async registerAgentAndAgency(@Body() request: CreateCompanyContactDto): Promise<CreateCompanyContactResponse> {
try {
this.logger.log(`Registering agent and agency via API: ${request.companyName} - ${request.contactEmail}`);

return await this.registrationService.registerAgentAndAgency(request);
} catch (error) {
this.logger.error(`Error creating company and contact via API: ${request.companyName}`, error);
throw error;
}
}

/**
* Check Tipalti completion status for all companies with payees
* Only available in development environment
* POST /test/registration/tipalti-status-check
*/
@UseGuards(DevelopmentOnlyGuard)
@Post('registration/tipalti-status-check')
async checkTipaltiCompletionStatus(): Promise<CheckTipaltiCompletionStatusResponse> {
try {
this.logger.log('Checking Tipalti completion status via API');

return await this.registrationService.checkTipaltiCompletionStatus();
} catch (error) {
this.logger.error('Error checking Tipalti completion status via API:', error);
throw error;
}
}
}
189 changes: 189 additions & 0 deletions test2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { Body, Controller, Get, Logger, Param, Post, Query, UseGuards } from '@nestjs/common';
import { ThirdPartyApiClientService } from '@/common/third-party-api-client.service';
import { ApifyClientService } from '@/common/apify-client.service';
import { DevelopmentOnlyGuard } from '@/common/guards/development-only.guard';
import { APIFY_JOB_STATUSES } from '@/constants/registration.constants';
import { ApifyPromptPilotDatasetItem } from '@/types/apify.types';
import { CreateCompanyContactDto, CreateCompanyContactResponse } from '@/modules/registration/dto/create-company-contact.dto';
import { JobStatusService } from '@/modules/registration/job-status.service';
import { RegistrationService } from '@/modules/registration/registration.service';
import { IataRegistrationCheckResponse } from '@/types/registration-job-status.types';
import { CheckTipaltiCompletionStatusResponse } from '@/types/tipalti-status-check.types';

@Controller('test')
export class TestController {
private readonly logger = new Logger(TestController.name);

constructor(
private readonly thirdPartyApiClient: ThirdPartyApiClientService,
private readonly apifyClientService: ApifyClientService,
private readonly jobStatusService: JobStatusService,
private readonly registrationService: RegistrationService,
) {}

/**
* Search Ventrata resellers by IATA code
* Only available in development environment
* @param iataCode - The IATA code to search for
*/
@UseGuards(DevelopmentOnlyGuard)
@Get('ventrata/resellers/search')
async searchVentrataResellers(@Query('iataCode') iataCode: string) {
if (!iataCode) {
return {
success: false,
message: 'iataCode query parameter is required',
};
}

try {
this.logger.log(`API call: Searching Ventrata resellers for IATA code: ${iataCode}`);

const result = await this.thirdPartyApiClient.searchVentrataResellers(iataCode);

return {
success: result.success,
data: result.data,
exists: result.exists,
message: result.success ? `Found ${result.data.length} resellers for IATA code ${iataCode}` : result.error || 'Search failed',
};
} catch (error) {
this.logger.error(`API call failed for IATA code ${iataCode}:`, error);
return {
success: false,
message: `Search failed: ${error.message}`,
};
}
}

/**
* Trigger Ventrata registration step (dev-only)
*/
@UseGuards(DevelopmentOnlyGuard)
@Post('ventrata/registration/:registrationId?')
async executeVentrataRegistration(@Body() request: CreateCompanyContactDto, @Param('registrationId') registrationId?: string) {
const registrationIdNumber = registrationId ? Number(registrationId) : undefined;
return this.thirdPartyApiClient.executeVentrataRegistration(request, registrationIdNumber);
}

/**
* Get Apify run status by run ID
* Only available in development environment
* @param runId - The Apify run ID to check status for
*/
@UseGuards(DevelopmentOnlyGuard)
@Get('apify/run-status')
async getApifyRunStatus(@Query('runId') runId: string) {
if (!runId) {
return {
success: false,
message: 'runId query parameter is required',
};
}

try {
this.logger.log(`API call: Getting Apify run status for runId: ${runId}`);

const runResult = await this.apifyClientService.getRunStatus(runId);
const apifyStatus = runResult.status;

if (apifyStatus === APIFY_JOB_STATUSES.SUCCEEDED) {
if (runResult.defaultDatasetId) {
const datasetItems = await this.apifyClientService.getDatasetItems<ApifyPromptPilotDatasetItem>(runResult.defaultDatasetId, 1);
let result = true;
if (datasetItems.length === 0) {
result = false;
this.logger.warn(`No dataset items found for succeeded run ${runId}, treating as failed`);
}

// If error exists and error doesn't contain 'panelAgencyResult2Columns', we treat it as failed
if (datasetItems[0] && datasetItems[0].error && !datasetItems[0].error?.includes('panelAgencyResult2Columns')) {
result = false;
this.logger.warn(`Dataset item contains error without 'panelAgencyResult2Columns' for jobId ${runId}, treating as failed`);
}
return {
success: result,
data: datasetItems,
message: `Retrieved run status: ${runResult.status} for runId ${runId}`,
};
}
}

return {
success: false,
data: null,
message: `Retrieved run status: ${runResult.status} for runId ${runId}`,
};
} catch (error) {
this.logger.error(`API call failed for runId ${runId}:`, error);
return {
success: false,
message: `Failed to get run status: ${error.message}`,
};
}
}

/**
* Check job statuses for registrations with READY job status and PENDING registration status
* Only available in development environment
* POST /test/registration/job-status/check
*/
@UseGuards(DevelopmentOnlyGuard)
@Post('registration/job-status/check')
async checkJobStatuses(): Promise<IataRegistrationCheckResponse> {
try {
this.logger.log('Checking job statuses via API');

const results = await this.jobStatusService.checkJobStatuses();

return {
success: true,
data: results,
message: `Checked ${results.length} job statuses`,
};
} catch (error) {
this.logger.error('Error checking job statuses via API:', error);
return {
success: false,
data: [],
message: `Failed to check job statuses: ${error instanceof Error ? error.message : String(error)}`,
};
}
}

/**
* Register agent and agency in HubSpot (second step of registration form)
* Only available in development environment
* POST /test/registration/workflow
*/
@UseGuards(DevelopmentOnlyGuard)
@Post('registration/workflow')
async registerAgentAndAgency(@Body() request: CreateCompanyContactDto): Promise<CreateCompanyContactResponse> {
try {
this.logger.log(`Registering agent and agency via API: ${request.companyName} - ${request.contactEmail}`);

return await this.registrationService.registerAgentAndAgency(request);
} catch (error) {
this.logger.error(`Error creating company and contact via API: ${request.companyName}`, error);
throw error;
}
}

/**
* Check Tipalti completion status for all companies with payees
* Only available in development environment
* POST /test/registration/tipalti-status-check
*/
@UseGuards(DevelopmentOnlyGuard)
@Post('registration/tipalti-status-check')
async checkTipaltiCompletionStatus(): Promise<CheckTipaltiCompletionStatusResponse> {
try {
this.logger.log('Checking Tipalti completion status via API');

return await this.registrationService.checkTipaltiCompletionStatus();
} catch (error) {
this.logger.error('Error checking Tipalti completion status via API:', error);
throw error;
}
}
}