-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetCompanyIncomeStatement.ts
More file actions
76 lines (61 loc) · 2.92 KB
/
getCompanyIncomeStatement.ts
File metadata and controls
76 lines (61 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { GetCompanyIncomeStatement, IncomeStatementResponse } from '../types';
import onAirRequest, { IncomeStatementApiResponse } from './onAirRequest';
import { isValidGuid } from '../utils';
import { AxiosResponse } from 'axios';
const endPoint = 'company/';
function subtractDays(dateStr:string = new Date().toISOString(), days = 30):Date{
const result = new Date(dateStr);
result.setDate(result.getDate() - days);
return result;
}
function addDays(dateStr:string = new Date().toISOString(), days = 30):Date{
const result = new Date(dateStr);
result.setDate(result.getDate() + days);
return result;
}
export const getCompanyIncomeStatement:GetCompanyIncomeStatement = async (companyId: string, apiKey: string, startDate?: string, endDate?: string):Promise<IncomeStatementResponse> => {
if (!companyId) throw new Error('No Company Id provided');
if (!apiKey) throw new Error('No Api Key provided');
if (!isValidGuid(companyId)) throw new Error('Invalid Company Id provided');
if (!isValidGuid(apiKey)) throw new Error('Invalid Api Key provided');
try {
const currentDate:Date = new Date();
const currentDateStr:string = currentDate.toISOString();
let StartDateStr = '';
let EndDateStr = '';
// if the startDate variable exists, and the endDate variable is undefined
// set the endDate equal to the startDate minus 30 days
// otherwise if the startDate variable is undefined and the endDate is defined
// set the startDate equal to the endDate plus 30 days
if (startDate && !endDate) {
endDate = subtractDays(startDate, 30).toISOString();
}
else if (!startDate && endDate) {
startDate = addDays(endDate, 30).toISOString();
}
if (!startDate && !endDate) {
startDate = currentDateStr;
endDate = subtractDays(startDate, 30).toISOString();
}
if (!startDate) throw new Error('startDate is empty');
if (!endDate) throw new Error('endDate is empty');
StartDateStr = startDate;
EndDateStr = endDate;
const url = `https://server1.onair.company/api/v1/${endPoint}${companyId}/incomestatement?startDate=${StartDateStr}&endDate=${EndDateStr}`;
const oaResponse:AxiosResponse<IncomeStatementApiResponse, unknown> = await onAirRequest<IncomeStatementApiResponse>(
url,
apiKey,
);
if (typeof oaResponse.data.Content === 'undefined') {
throw new Error(oaResponse.data.Error ? oaResponse.data.Error : `Company Id "${companyId}"" not found`);
}
const response:IncomeStatementResponse = {
StartDate: StartDateStr,
EndDate: EndDateStr,
Content: oaResponse.data.Content,
};
return response;
} catch (e) {
throw new Error(e.oaResponse.status === 400 ? `Company Id "${companyId}"" not found` : e.message);
}
};