Skip to content

Commit

Permalink
fix: add proper data sources for other stat page metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
r4zendev committed Jan 16, 2025
1 parent b7adffb commit 7853872
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 17 deletions.
5 changes: 3 additions & 2 deletions apps/backoffice-v2/src/pages/Home/Home.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ export const Home: FunctionComponent = () => {
{/* <TabsContent value={defaultTabValue}>*/}
{/* </TabsContent>*/}
{/*</Tabs>*/}
{(isDemo || isExample) && <Outlet />}
{!isDemo && !isExample && <WelcomeCard />}
<Outlet />
{/* {(isDemo || isExample) && <Outlet />}
{!isDemo && !isExample && <WelcomeCard />} */}
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import {
} from '@/business-report/dtos/business-report-metrics.dto';
import { BusinessReportMetricsDto } from './dtos/business-report-metrics-dto';
import { FEATURE_LIST, TCustomerWithFeatures } from '@/customer/types';
import dayjs from 'dayjs';

@ApiBearerAuth()
@swagger.ApiTags('Business Reports')
Expand Down Expand Up @@ -167,19 +166,17 @@ export class BusinessReportControllerExternal {
@CurrentProject() currentProjectId: TProjectId,
@Query() { from, to }: BusinessReportMetricsRequestQueryDto,
) {
const { id: customerId } = await this.customerService.getByProjectId(currentProjectId);
const { id: customerId, features } = await this.customerService.getByProjectId(
currentProjectId,
);

const unmonitoredMerchants = await this.prismaService.business.count({
where: {
projectId: currentProjectId,
metadata: {
path: ['featureConfig', FEATURE_LIST.ONGOING_MERCHANT_REPORT, 'disabledAt'],
not: 'null',
...(from && { gt: dayjs(from).toDate().getTime() }),
...(to && { lte: dayjs(to).toDate().getTime() }),
},
},
});
const { totalActiveMerchants, addedMerchantsCount, unmonitoredMerchants } =
await this.businessService.getMerchantMonitoringMetrics({
projectIds: [currentProjectId],
features,
from,
to,
});

const merchantMonitoringMetrics = await this.merchantMonitoringClient.getMetrics({
customerId,
Expand All @@ -189,6 +186,8 @@ export class BusinessReportControllerExternal {

return {
...merchantMonitoringMetrics,
totalActiveMerchants,
addedMerchantsCount,
removedMerchantsCount: unmonitoredMerchants,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,13 @@ export class BusinessRepository {
...args,
});
}

async count<T extends Prisma.BusinessCountArgs>(
args: Prisma.SelectSubset<T, Prisma.BusinessCountArgs>,
projectIds: TProjectIds,
) {
return await this.prismaService.business.count(
this.scopeService.scopeFindMany(args, projectIds),
);
}
}
105 changes: 103 additions & 2 deletions services/workflows-service/src/business/business.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import {
TCompanyInformation,
} from '@/business/types/business-information';
import { AppLoggerService } from '@/common/app-logger/app-logger.service';
import { FEATURE_LIST, TCustomerFeaturesConfig, TCustomerWithFeatures } from '@/customer/types';
import { env } from '@/env';
import type { PrismaTransaction, TProjectIds } from '@/types';
import { HttpService } from '@nestjs/axios';
import * as common from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Business } from '@prisma/client';
import { Business, Prisma } from '@prisma/client';
import { AxiosError } from 'axios';
import { plainToClass } from 'class-transformer';
import dayjs from 'dayjs';
import { lastValueFrom } from 'rxjs';
import { z } from 'zod';
import { BusinessRepository } from './business.repository';
import { TCustomerWithFeatures } from '@/customer/types';

@Injectable()
export class BusinessService {
Expand Down Expand Up @@ -66,6 +68,105 @@ export class BusinessService {
return await this.repository.updateById(id, args, transaction);
}

async getMerchantMonitoringMetrics({
projectIds,
features,
from,
to,
}: {
projectIds: string[];
features: TCustomerWithFeatures['features'];
from: string | undefined;
to: string | undefined;
}): Promise<{
totalActiveMerchants: number;
addedMerchantsCount: number;
unmonitoredMerchants: number;
}> {
// Metrics are currently mostly requested by month
if (!from) {
from = dayjs().startOf('month').toISOString();
}

if (!to) {
to = dayjs(from).add(1, 'month').toISOString();
}

const allProjectMerchants = await this.repository.findMany({}, projectIds);

const totalActiveMerchants = allProjectMerchants.filter(b => {
const disabledAt = z
.number()
.nullable()
.catch(() => null)
.parse(
(
b.metadata as {
featureConfig: Record<
(typeof FEATURE_LIST)[keyof typeof FEATURE_LIST],
TCustomerFeaturesConfig & { disabledAt: number | null | undefined }
>;
}
)?.featureConfig?.[FEATURE_LIST.ONGOING_MERCHANT_REPORT]?.disabledAt,
);

return (
disabledAt === null ||
(b.metadata === null && features?.ONGOING_MERCHANT_REPORT?.options?.runByDefault)
);
}).length;

const addedMerchantsCount = await this.repository.count(
{
where: {
OR: [
{
metadata: {
path: ['featureConfig', FEATURE_LIST.ONGOING_MERCHANT_REPORT, 'disabledAt'],
equals: Prisma.AnyNull,
},
},
features?.ONGOING_MERCHANT_REPORT?.options?.runByDefault
? { metadata: { equals: Prisma.AnyNull } }
: {},
],
createdAt: {
gte: dayjs(from).toISOString(),
lt: dayjs(to).toISOString(),
},
},
},
projectIds,
);

const unmonitoredMerchants = await this.repository.count(
{
where: {
OR: [
{
metadata: {
path: ['featureConfig', FEATURE_LIST.ONGOING_MERCHANT_REPORT, 'disabledAt'],
not: 'null',
gte: dayjs(from).toDate().getTime(),
lt: dayjs(to).toDate().getTime(),
},
},
!features?.ONGOING_MERCHANT_REPORT?.options?.runByDefault
? { metadata: { equals: Prisma.AnyNull } }
: {},
],
},
},
projectIds,
);

return {
totalActiveMerchants,
addedMerchantsCount,
unmonitoredMerchants,
};
}

async fetchCompanyInformation({
registrationNumber,
jurisdictionCode,
Expand Down

0 comments on commit 7853872

Please sign in to comment.