Skip to content

Commit 3837f4a

Browse files
committed
feat: supply option to use from and to in request
1 parent d1aa5b6 commit 3837f4a

File tree

8 files changed

+55
-50
lines changed

8 files changed

+55
-50
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export DEBITOOR_API_TOKEN="<debitoor_api_token>"
2020
// Optional - run for a specific range
2121
"range": {
2222
"month": 8,
23-
"year": 2020
23+
"year": 2020,
24+
"from": "ISODATE",
25+
"to": "ISODATE"
2426
},
2527
// Optional - run only for specific customers
2628
"customerWhitelist": [

src/functions/debitoor/handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const handler: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
4040
} = config;
4141

4242
const timeEntries = await fetchTimeEntriesBetween(
43-
time.startOfMonthISO,
44-
time.endOfMonthISO
43+
time.fromAsISO,
44+
time.toAsISO
4545
);
4646

4747
const billableTimeEntries = filterTimeEntriesByLabel(

src/functions/schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export default {
4444
year: {
4545
type: 'integer',
4646
},
47+
from: {
48+
type: 'string',
49+
},
50+
to: {
51+
type: 'string',
52+
},
4753
},
4854
},
4955
},

src/functions/sheet/handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const handler: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
3232
} = config;
3333

3434
const timeEntries = await fetchTimeEntriesBetween(
35-
time.startOfMonthISO,
36-
time.endOfMonthISO
35+
time.fromAsISO,
36+
time.toAsISO
3737
);
3838

3939
const billableTimeEntries = filterTimeEntriesByLabel(

src/libs/csv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ export const createCsv = async (
5454
mapWrapJoin(
5555
translate('FROM'),
5656
formatDateForInvoice(
57-
config.time.startOfMonthFormatted,
57+
config.time.fromDateFormatted,
5858
customer.contentful.language
5959
)
6060
),
6161
mapWrapJoin(
6262
translate('TO'),
6363
formatDateForInvoice(
64-
config.time.endOfMonthFormatted,
64+
config.time.toDateFormatted,
6565
customer.contentful.language
6666
)
6767
),

src/libs/debitoor.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import {
1717
LogoResponse,
1818
Product,
1919
Settings,
20-
Unit,
20+
Unit
2121
} from './debitoor-types';
2222
import { formatDateForInvoice, getRoundedHours } from './time';
2323
import {
2424
ClientTimeEntries,
2525
EnrichedTimeEntry,
26-
ProjectTimeEntries,
26+
ProjectTimeEntries
2727
} from './toggl-types';
2828
import { CustomerData, Locale } from './types';
2929
import {
@@ -32,7 +32,7 @@ import {
3232
initFetch,
3333
initTranslate,
3434
Logger,
35-
uniquify,
35+
uniquify
3636
} from './utils';
3737

3838
const BASE_URL = 'https://api.debitoor.com/api';
@@ -264,11 +264,11 @@ export const generateInvoiceTemplate = async (
264264
const notes = [
265265
translate('PERFORMANCE_PERIOD', {
266266
from: formatDateForInvoice(
267-
time.startOfMonthFormatted,
267+
time.fromDateFormatted,
268268
customerTimeEntries.customer.contentful.language
269269
),
270270
to: formatDateForInvoice(
271-
time.endOfMonthFormatted,
271+
time.toDateFormatted,
272272
customerTimeEntries.customer.contentful.language
273273
),
274274
}),
@@ -349,11 +349,11 @@ export const createDraftInvoices = async (
349349
const fileName = [
350350
matches.join(''),
351351
formatDateForInvoice(
352-
config.time.startOfMonthFormatted,
352+
config.time.fromDateFormatted,
353353
customerTimeEntries.customer.contentful.language
354354
),
355355
formatDateForInvoice(
356-
config.time.endOfMonthFormatted,
356+
config.time.toDateFormatted,
357357
customerTimeEntries.customer.contentful.language
358358
),
359359
].join('_');

src/libs/time.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,50 +34,47 @@ export const sortByDate = <T>(a: T, b: T) =>
3434
moment(a['stop'] ?? a).isBefore(moment(b['stop'] ?? b)) ? -1 : 1;
3535

3636
export class Time {
37+
public from: moment.Moment;
38+
public to: moment.Moment;
3739
private _moment: moment.Moment = moment();
3840
private _format: string = 'YYYY-MM-DD';
39-
constructor(public month?: number, public year?: number) {
40-
if (this.month !== undefined) {
41-
this._moment.month(this.month - 1);
41+
42+
constructor(
43+
private config: {
44+
month?: number;
45+
year?: number;
46+
from?: string;
47+
to?: string;
4248
}
43-
if (this.year !== undefined) {
44-
this._moment.year(this.year);
49+
) {
50+
if (this.config.from && this.config.to) {
51+
this.from = moment(this.config.from);
52+
this.to = moment(this.config.to);
53+
} else {
54+
if (this.config.month !== undefined) {
55+
this._moment.month(this.config.month - 1);
56+
}
57+
if (this.config.year !== undefined) {
58+
this._moment.year(this.config.year);
59+
}
60+
this.from = this._moment.clone().startOf('month');
61+
this.to = this._moment.clone().endOf('month');
4562
}
4663
}
4764

48-
get startOfMonth(): moment.Moment {
49-
return this._moment.clone().startOf('month');
50-
}
51-
52-
get endOfMonth(): moment.Moment {
53-
return this._moment.clone().endOf('month');
54-
}
55-
56-
get startOfMonthFormatted(): string {
57-
return this.startOfMonth.format(this._format);
58-
}
59-
60-
get endOfMonthFormatted(): string {
61-
return this.endOfMonth.format(this._format);
62-
}
63-
64-
get startOfMonthISO(): string {
65-
return this.startOfMonth.format();
66-
}
67-
68-
get endOfMonthISO(): string {
69-
return this.endOfMonth.format();
65+
get fromDateFormatted(): string {
66+
return this.from.format(this._format);
7067
}
7168

72-
get startOfMonthEpoch(): number {
73-
return this.startOfMonth.valueOf();
69+
get toDateFormatted(): string {
70+
return this.to.format(this._format);
7471
}
7572

76-
get endOfMonthEpoch(): number {
77-
return this.endOfMonth.valueOf();
73+
get fromAsISO(): string {
74+
return this.from.format();
7875
}
7976

80-
get dueTimestamp(): string {
81-
return this.endOfMonth.utc().add(1, 'day').format();
77+
get toAsISO(): string {
78+
return this.to.format();
8279
}
8380
}

src/libs/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ export const getConfig = <T extends EventBody>(
110110
const month =
111111
body?.range?.month ?? (usePreviousMonth ? getLastMonth() : undefined);
112112

113-
const time = new Time(month, body?.range?.year);
113+
const time = new Time({ ...(body?.range || {}), month });
114114

115115
const isProduction = getEnvironment() === 'production';
116116
return {
117117
...(body || {}),
118118
range: {
119-
from: time.startOfMonthFormatted,
120-
to: time.endOfMonthFormatted,
119+
from: time.fromDateFormatted,
120+
to: time.toDateFormatted,
121121
},
122122
dryRun: body?.dryRun ?? !isProduction,
123123
setBilled: body?.setBilled ?? isProduction,

0 commit comments

Comments
 (0)