Skip to content

Commit c704f71

Browse files
feat(monitoring): implement ongoing monitoring state update functionality
- Add route for updating ongoing monitoring state using Patch method - Introduce OngoingMonitoringPatchDto with updated status options - Refactor related business logic and metadata handling in the controller
1 parent 4b8d1a2 commit c704f71

File tree

6 files changed

+37
-56
lines changed

6 files changed

+37
-56
lines changed

services/workflows-service/src/business-report/business-report.controller.external.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
import { BusinessReportMetricsDto } from './dtos/business-report-metrics-dto';
4343
import { BusinessReportStatusUpdateRequestParamsDto } from '@/business-report/dtos/business-report-status-update.dto';
4444
import { UserData } from '@/user/user-data.decorator';
45+
import { OngoingMonitoringPatchDto } from './dtos/ongoing-monitoring.patch.dto';
4546

4647
@ApiBearerAuth()
4748
@swagger.ApiTags('Business Reports')
@@ -442,4 +443,22 @@ export class BusinessReportControllerExternal {
442443
res.setHeader('content-type', 'application/json');
443444
res.send(result);
444445
}
446+
447+
@common.Patch('/websites/:websiteId/monitoring')
448+
@swagger.ApiForbiddenResponse()
449+
@swagger.ApiOkResponse()
450+
@swagger.ApiNotFoundResponse({ type: errors.NotFoundException })
451+
async updateOngoingMonitoringState(
452+
@common.Param('websiteId') websiteId: string,
453+
@common.Body() data: OngoingMonitoringPatchDto,
454+
@CurrentProject() currentProjectId: TProjectId,
455+
) {
456+
const { id: customerId } = await this.customerService.getByProjectId(currentProjectId);
457+
458+
await this.merchantMonitoringClient.updateOngoingMonitoring({
459+
customerId,
460+
websiteId,
461+
status: data.status,
462+
});
463+
}
445464
}

services/workflows-service/src/business/dtos/business-monitoring.patch.dto.ts renamed to services/workflows-service/src/business-report/dtos/ongoing-monitoring.patch.dto.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ApiProperty } from '@nestjs/swagger';
22
import { IsIn, IsString } from 'class-validator';
33

4-
export class BusinessMonitoringPatchDto {
4+
export class OngoingMonitoringPatchDto {
55
@ApiProperty({ type: String, required: true })
66
@IsString()
7-
@IsIn(['on', 'off'])
8-
state!: 'on' | 'off';
7+
@IsIn(['active', 'inactive'])
8+
status!: 'active' | 'inactive';
99
}

services/workflows-service/src/business/business.controller.external.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as swagger from '@nestjs/swagger';
55
import { ApiBearerAuth, ApiExcludeEndpoint } from '@nestjs/swagger';
66
import { plainToClass } from 'class-transformer';
77
import type { Request } from 'express';
8-
import _ from 'lodash';
98

109
import { BusinessInformation } from '@/business/dtos/business-information';
1110
import { BusinessDto } from '@/business/dtos/business.dto';
@@ -15,7 +14,6 @@ import { CurrentProject } from '@/common/decorators/current-project.decorator';
1514
import { ProjectIds } from '@/common/decorators/project-ids.decorator';
1615
import { UseCustomerAuthGuard } from '@/common/decorators/use-customer-auth-guard.decorator';
1716
import { UseKeyAuthOrSessionGuard } from '@/common/decorators/use-key-auth-or-session-guard.decorator';
18-
import { FEATURE_LIST, TCustomerWithFeatures } from '@/customer/types';
1917
import { PrismaService } from '@/prisma/prisma.service';
2018
import { isRecordNotFoundError } from '@/prisma/prisma.util';
2119
import type { TProjectId, TProjectIds } from '@/types';
@@ -29,7 +27,6 @@ import { BusinessModel } from './business.model';
2927
import { BusinessService } from './business.service';
3028
import { BusinessCreateDto } from './dtos/business-create';
3129
import { BusinessFindManyArgs } from './dtos/business-find-many-args';
32-
import { BusinessMonitoringPatchDto } from './dtos/business-monitoring.patch.dto';
3330
import { BusinessWhereUniqueInput } from './dtos/business-where-unique-input';
3431

3532
@ApiBearerAuth()
@@ -134,42 +131,6 @@ export class BusinessControllerExternal {
134131
});
135132
}
136133

137-
@common.Patch('/:id/monitoring')
138-
@swagger.ApiForbiddenResponse()
139-
@swagger.ApiOkResponse({ type: BusinessDto })
140-
@swagger.ApiNotFoundResponse({ type: errors.NotFoundException })
141-
async updateOngoingMonitoringState(
142-
@common.Param('id') businessId: string,
143-
@common.Body() data: BusinessMonitoringPatchDto,
144-
@CurrentProject() currentProjectId: TProjectId,
145-
) {
146-
const business = await this.businessService.getById(
147-
businessId,
148-
{ select: { metadata: true } },
149-
[currentProjectId],
150-
);
151-
152-
const metadata = business?.metadata as {
153-
featureConfig?: TCustomerWithFeatures['features'];
154-
};
155-
156-
const isEnabled = data.state === 'on';
157-
const updatedMetadata = _.merge({}, metadata, {
158-
featureConfig: {
159-
[FEATURE_LIST.ONGOING_MERCHANT_REPORT]: {
160-
enabled: isEnabled,
161-
disabledAt: isEnabled ? null : new Date().getTime(),
162-
},
163-
},
164-
});
165-
166-
await this.businessService.updateById(businessId, {
167-
data: {
168-
metadata: updatedMetadata,
169-
},
170-
});
171-
}
172-
173134
@common.Patch(':id')
174135
@UseCustomerAuthGuard()
175136
@swagger.ApiForbiddenResponse()

services/workflows-service/src/common/utils/unified-api-client/unified-api-client.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,11 @@ export class UnifiedApiClient {
165165
}
166166

167167
public formatBusiness(business: BusinessPayload) {
168-
const metadata = business.metadata as unknown as {
169-
featureConfig: TCustomerWithFeatures['features'];
170-
lastOngoingReportInvokedAt: number;
171-
} | null;
172-
173-
const unsubscribedMonitoringAt = metadata?.featureConfig?.[FEATURE_LIST.ONGOING_MERCHANT_REPORT]
174-
?.disabledAt
175-
? new Date(metadata.featureConfig[FEATURE_LIST.ONGOING_MERCHANT_REPORT]!.disabledAt!)
176-
: metadata?.featureConfig?.[FEATURE_LIST.ONGOING_MERCHANT_REPORT]?.enabled === false
177-
? new Date()
178-
: null;
179-
180168
return {
181169
id: business.id,
182170
correlationId: business.correlationId,
183171
companyName: business.companyName,
184172
customerId: business.project.customer.id,
185-
unsubscribedMonitoringAt: unsubscribedMonitoringAt?.toISOString() ?? null,
186173
createdAt: business.createdAt.toISOString(),
187174
updatedAt: business.updatedAt.toISOString(),
188175
};

services/workflows-service/src/merchant-monitoring/merchant-monitoring.client.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,20 @@ export class MerchantMonitoringClient {
273273
});
274274
}
275275

276+
public async updateOngoingMonitoring({
277+
customerId,
278+
websiteId,
279+
status,
280+
}: {
281+
customerId: string;
282+
websiteId: string;
283+
status: 'active' | 'inactive';
284+
}) {
285+
await this.axios.put(`customers/${customerId}/websites/${websiteId}`, {
286+
unsubscribedMonitoringAt: status === 'active' ? null : new Date().toISOString(),
287+
});
288+
}
289+
276290
public async getMetrics({
277291
customerId,
278292
from,

0 commit comments

Comments
 (0)