Skip to content

Commit 24c143d

Browse files
Refactor fetchFinancialAppData to Integrate QBO Effective Revenue
- Updated the `fetchFinancialAppData` function to utilize the `QBORepository` for fetching effective revenue data, enhancing the accuracy of financial reports. - Adjusted the handling of effective revenue in the report generation process to ensure it correctly maps to project IDs. - Added necessary mocks for `QBORepository` in the corresponding test file to ensure proper testing of the new functionality. These changes improve the integration of financial data from QuickBooks Online, providing more reliable insights into project performance.
1 parent c57099e commit 24c143d

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

workers/main/src/activities/weeklyFinancialReports/fetchFinancialAppData.test.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { AppError } from '../../common/errors';
55
import * as fileUtils from '../../common/fileUtils';
66
import * as mongoPoolModule from '../../common/MongoPool';
77
import type { TargetUnit } from '../../common/types';
8-
import type { Employee, Project } from '../../services/FinApp';
9-
import type { IFinAppRepository } from '../../services/FinApp';
8+
import type { Employee, IFinAppRepository, Project } from '../../services/FinApp';
109
import * as finAppService from '../../services/FinApp';
10+
import type { CustomerRevenueByRef } from '../../services/QBO';
11+
import * as qboService from '../../services/QBO';
1112
import { fetchFinancialAppData } from './fetchFinancialAppData';
1213

1314
type MongoPoolMock = {
@@ -30,6 +31,9 @@ vi.mock('../../common/MongoPool', () => ({
3031
vi.mock('../../services/FinApp', () => ({
3132
FinAppRepository: vi.fn(),
3233
}));
34+
vi.mock('../../services/QBO', () => ({
35+
QBORepository: vi.fn(),
36+
}));
3337

3438
const mockTargetUnits: TargetUnit[] = [
3539
{
@@ -49,11 +53,19 @@ const mockEmployees: Employee[] = [
4953
const mockProjects: Project[] = [
5054
{
5155
redmine_id: 2,
52-
quick_books_id: 10,
56+
quick_books_id: '10',
5357
history: { rate: { '2024-01-01': 200 } },
5458
},
5559
];
5660

61+
const mockEffectiveRevenue: CustomerRevenueByRef = {
62+
'10': {
63+
customerName: 'Test Customer',
64+
totalAmount: 5000,
65+
invoiceCount: 3,
66+
},
67+
};
68+
5769
function createRepoInstance(
5870
overrides: Partial<IFinAppRepository> = {},
5971
): IFinAppRepository {
@@ -83,8 +95,10 @@ describe('getFinAppData', () => {
8395
let connect: Mock;
8496
let disconnect: Mock;
8597
let FinAppRepository: Mock;
98+
let QBORepository: Mock;
8699
let dateSpy: ReturnType<typeof vi.spyOn>;
87100
let repoInstance: IFinAppRepository;
101+
let qboRepoInstance: { getEffectiveRevenue: Mock };
88102
let mongoPoolInstance: MongoPoolMock;
89103

90104
const fileLink = 'input.json';
@@ -100,6 +114,7 @@ describe('getFinAppData', () => {
100114
(repoInstance.getProjectsByRedmineIds as Mock).mockResolvedValue(
101115
mockProjects,
102116
);
117+
qboRepoInstance.getEffectiveRevenue.mockResolvedValue(mockEffectiveRevenue);
103118
}
104119

105120
async function expectAppError(promise: Promise<unknown>, msg: string) {
@@ -113,10 +128,16 @@ describe('getFinAppData', () => {
113128
readJsonFile = vi.mocked(fileUtils.readJsonFile);
114129
writeJsonFile = vi.mocked(fileUtils.writeJsonFile);
115130
FinAppRepository = vi.mocked(finAppService.FinAppRepository);
131+
QBORepository = vi.mocked(qboService.QBORepository);
116132

117133
repoInstance = createRepoInstance();
118134
FinAppRepository.mockImplementation(() => repoInstance);
119135

136+
qboRepoInstance = {
137+
getEffectiveRevenue: vi.fn().mockResolvedValue(mockEffectiveRevenue),
138+
};
139+
QBORepository.mockImplementation(() => qboRepoInstance);
140+
120141
connect = vi.fn().mockResolvedValue(undefined);
121142
disconnect = vi.fn().mockResolvedValue(undefined);
122143
mongoPoolInstance = createMongoPoolInstance(connect, disconnect);
@@ -141,13 +162,19 @@ describe('getFinAppData', () => {
141162
expect(readJsonFile).toHaveBeenCalledWith(fileLink);
142163
expect(writeJsonFile).toHaveBeenCalledWith(expectedFilename, {
143164
employees: mockEmployees,
144-
projects: mockProjects,
165+
projects: [
166+
{
167+
...mockProjects[0],
168+
effectiveRevenue: 5000,
169+
},
170+
],
171+
effectiveRevenue: mockEffectiveRevenue,
145172
});
146173
});
147174

148175
it('always disconnects the mongo pool', async () => {
149176
setupSuccessMocks();
150-
await fetchFinancialAppData(fileLink).catch(() => {});
177+
await fetchFinancialAppData(fileLink).catch(() => { });
151178
expect(() => mongoPoolInstance.disconnect()).not.toThrow();
152179
});
153180
});

workers/main/src/activities/weeklyFinancialReports/fetchFinancialAppData.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,23 @@ export const fetchFinancialAppData = async (
2828
const employeeIds = getUniqueIds(targetUnits, 'user_id');
2929
const projectIds = getUniqueIds(targetUnits, 'project_id');
3030

31-
const [employees, projects, effectiveRevenue] = await Promise.all([
32-
repo.getEmployeesByRedmineIds(employeeIds),
33-
repo.getProjectsByRedmineIds(projectIds),
34-
qboRepo.getEffectiveRevenue(),
35-
]);
31+
const [employees, projects, effectiveRevenueByCustomerRef] =
32+
await Promise.all([
33+
repo.getEmployeesByRedmineIds(employeeIds),
34+
repo.getProjectsByRedmineIds(projectIds),
35+
qboRepo.getEffectiveRevenue(),
36+
]);
3637

3738
await writeJsonFile(filename, {
3839
employees,
3940
projects: projects.map((project) => ({
4041
...project,
4142
effectiveRevenue: project.quick_books_id
42-
? effectiveRevenue[project.quick_books_id]?.totalAmount || 0
43+
? effectiveRevenueByCustomerRef[project.quick_books_id]
44+
?.totalAmount || 0
4345
: 0,
4446
})),
45-
effectiveRevenue,
47+
effectiveRevenue: effectiveRevenueByCustomerRef,
4648
});
4749

4850
return { fileLink: filename };

0 commit comments

Comments
 (0)