Skip to content

Commit 58e4370

Browse files
refactor(workflows): improve financial report workflow and error handling
- Updated the `weeklyFinancialReportsWorkflow` to accept a customizable period parameter, enhancing flexibility. - Introduced a new `fetchFinancialData` function to encapsulate financial data retrieval logic, improving code organization. - Enhanced error handling in the main worker script by simplifying the error handling function call. - Refactored the validation schema in `worker.ts` to use optional environment variables for better configuration management. These changes enhance the maintainability and usability of the financial reporting workflow, allowing for more dynamic data handling and improved error management.
1 parent e9a7431 commit 58e4370

File tree

6 files changed

+55
-27
lines changed

6 files changed

+55
-27
lines changed

workers/main/src/__tests__/weeklyFinancialReports.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ import * as utils from '../../../common/utils';
44
import { weeklyFinancialReportsWorkflow } from '../workflows';
55

66
describe('weeklyFinancialReportsWorkflow', () => {
7-
it('should return the report string', async () => {
7+
it('should return the report string with default parameters', async () => {
88
const result = await weeklyFinancialReportsWorkflow();
99

1010
expect(typeof result).toBe('string');
1111
expect(result.length).toBeGreaterThan(0);
1212
});
1313

14+
it('should return the report string for a custom period and config', async () => {
15+
const result = await weeklyFinancialReportsWorkflow({
16+
period: 'Q1 2025',
17+
config: { reportTitle: 'Custom Report Title' },
18+
});
19+
20+
expect(result.startsWith('Custom Report Title')).toBe(true);
21+
expect(result).toContain('Period: Q1 2025');
22+
});
23+
1424
it('should log and rethrow errors', async () => {
1525
const logSpy = vi
1626
.spyOn(utils, 'logWorkflowError')
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export interface FinancialData {
2+
period: string;
3+
contractType: string;
4+
revenue: number;
5+
cogs: number;
6+
margin: number;
7+
marginality: number;
8+
effectiveRevenue: number;
9+
effectiveMargin: number;
10+
effectiveMarginality: number;
11+
}
12+
13+
/**
14+
* Fetches financial data for a given period from an external source or database.
15+
* @param period - The period to fetch data for (e.g., 'Q1 2025', 'current')
16+
*/
17+
export async function fetchFinancialData(
18+
period: string = 'current',
19+
): Promise<FinancialData> {
20+
// TODO: Replace this stub with actual data fetching logic (e.g., DB query, API call)
21+
return {
22+
period: period,
23+
contractType: 'T&M',
24+
revenue: 120000,
25+
cogs: 80000,
26+
margin: 40000,
27+
marginality: 33.3,
28+
effectiveRevenue: 110000,
29+
effectiveMargin: 35000,
30+
effectiveMarginality: 31.8,
31+
};
32+
}

workers/main/src/configs/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import { z } from 'zod';
2-
31
import { temporalSchema } from './temporal';
42
import { workerSchema } from './worker';
53

6-
export const validationResult = z
7-
.object({
8-
...temporalSchema.shape,
9-
...workerSchema.shape,
10-
})
4+
export const validationResult = temporalSchema
5+
.merge(workerSchema)
116
.safeParse(process.env);

workers/main/src/configs/worker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { z } from 'zod';
44

55
export const workerConfig: WorkerOptions = {
66
taskQueue: 'main-queue',
7-
workflowsPath: path.join(__dirname, '../workflows'),
7+
workflowsPath:
8+
process.env.WORKFLOWS_PATH || path.join(__dirname, '../workflows'),
89
};
910

1011
export const workerSchema = z.object({
11-
TEMPORAL_ADDRESS: z.string().default('temporal:7233'),
12+
WORKFLOWS_PATH: z.string().optional(),
1213
});

workers/main/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function run(): Promise<void> {
2929

3030
await worker.run();
3131
} catch (err) {
32-
handleRunError(err as Error);
32+
handleRunError(err);
3333
} finally {
3434
if (connection) {
3535
await connection.close();

workers/main/src/workflows/weeklyFinancialReports/index.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
import { logWorkflowError } from '../../../../common/utils';
2+
import { fetchFinancialData } from '../../activities/fetchFinancialData';
23

3-
export async function weeklyFinancialReportsWorkflow(): Promise<string> {
4+
export async function weeklyFinancialReportsWorkflow({
5+
period = 'current',
6+
}: { period?: string } = {}): Promise<string> {
47
try {
5-
const data = {
6-
period: 'Q1 2025',
7-
contractType: 'T&M',
8-
revenue: 120000,
9-
cogs: 80000,
10-
margin: 40000,
11-
marginality: 33.3,
12-
effectiveRevenue: 110000,
13-
effectiveMargin: 35000,
14-
effectiveMarginality: 31.8,
15-
};
16-
178
const reportTitle = 'Weekly Financial Report';
9+
const data = await fetchFinancialData(period);
1810
const report = `Period: ${data.period}
1911
Contract Type: ${data.contractType}
2012
Revenue: $${data.revenue.toLocaleString()}
2113
COGS: $${data.cogs.toLocaleString()}
2214
Margin: $${data.margin.toLocaleString()}
23-
Marginality: ${data.marginality}%
24-
25-
Effective Revenue (last 4 months): $${data.effectiveRevenue.toLocaleString()}
15+
Marginality: ${data.marginality}%\n\nEffective Revenue (last 4 months): $${data.effectiveRevenue.toLocaleString()}
2616
Effective Margin: $${data.effectiveMargin.toLocaleString()}
2717
Effective Marginality: ${data.effectiveMarginality}%`;
2818

0 commit comments

Comments
 (0)