Skip to content

Commit 548e5ae

Browse files
helper date, add helper
1 parent 910d486 commit 548e5ae

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ack-nestjs-boilerplate",
3-
"version": "4.2.3",
3+
"version": "4.2.4",
44
"description": "Ack NestJs Boilerplate",
55
"repository": {
66
"type": "git",

src/common/helper/interfaces/helper.date-service.interface.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
IHelperDateOptionsDiff,
66
IHelperDateOptionsFormat,
77
IHelperDateOptionsForward,
8+
IHelperDateStartAndEnd,
9+
IHelperDateStartAndEndDate,
810
} from 'src/common/helper/interfaces/helper.interface';
911

1012
export interface IHelperDateService {
@@ -90,4 +92,8 @@ export interface IHelperDateService {
9092
startOfDay(date?: Date): Date;
9193

9294
extractDate(date: string | Date | number): IHelperDateExtractDate;
95+
96+
getStartAndEndDate(
97+
options?: IHelperDateStartAndEnd
98+
): IHelperDateStartAndEndDate;
9399
}

src/common/helper/interfaces/helper.interface.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ export interface IHelperGeoRules extends IHelperGeoCurrent {
4141
}
4242

4343
// Helper Date
44+
export interface IHelperDateStartAndEnd {
45+
month?: number;
46+
year?: number;
47+
}
48+
49+
export interface IHelperDateStartAndEndDate {
50+
startDate: Date;
51+
endDate: Date;
52+
}
53+
4454
export interface IHelperDateExtractDate {
4555
date: Date;
4656
day: string;

src/common/helper/services/helper.date.service.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
IHelperDateOptionsFormat,
1414
IHelperDateOptionsForward,
1515
IHelperDateOptionsRoundDown,
16+
IHelperDateStartAndEnd,
17+
IHelperDateStartAndEndDate,
1618
} from 'src/common/helper/interfaces/helper.interface';
1719

1820
@Injectable()
@@ -213,4 +215,35 @@ export class HelperDateService implements IHelperDateService {
213215

214216
return mDate.toDate();
215217
}
218+
219+
getStartAndEndDate(
220+
options?: IHelperDateStartAndEnd
221+
): IHelperDateStartAndEndDate {
222+
const today = moment();
223+
const todayMonth = Number(
224+
today.format(ENUM_HELPER_DATE_FORMAT.ONLY_MONTH)
225+
);
226+
const todayYear = Number(
227+
today.format(ENUM_HELPER_DATE_FORMAT.ONLY_YEAR)
228+
);
229+
230+
// set month and year
231+
const year = options?.year ?? todayYear;
232+
const month = options?.month ?? todayMonth;
233+
234+
const date = moment(`${year}-${month}-02`);
235+
let startDate: Date = date.startOf('year').toDate();
236+
let endDate: Date = date.endOf('year').toDate();
237+
238+
if (options?.month) {
239+
const date = moment(`${year}-${month}-02`);
240+
startDate = date.startOf('month').toDate();
241+
endDate = date.endOf('month').toDate();
242+
}
243+
244+
return {
245+
startDate,
246+
endDate,
247+
};
248+
}
216249
}

test/unit/helper/helper.date.service.spec.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import {
55
ENUM_HELPER_DATE_FORMAT,
66
} from 'src/common/helper/constants/helper.enum.constant';
77
import { HelperModule } from 'src/common/helper/helper.module';
8-
import { IHelperDateExtractDate } from 'src/common/helper/interfaces/helper.interface';
8+
import {
9+
IHelperDateExtractDate,
10+
IHelperDateStartAndEndDate,
11+
} from 'src/common/helper/interfaces/helper.interface';
912
import { HelperDateService } from 'src/common/helper/services/helper.date.service';
1013
import configs from 'src/configs';
1114

@@ -912,4 +915,60 @@ describe('HelperDateService', () => {
912915
expect(result).toBeTruthy();
913916
});
914917
});
918+
919+
describe('getStartAndEndDate', () => {
920+
it('should be success', async () => {
921+
const result: IHelperDateStartAndEndDate =
922+
helperDateService.getStartAndEndDate();
923+
924+
jest.spyOn(
925+
helperDateService,
926+
'getStartAndEndDate'
927+
).mockReturnValueOnce(result);
928+
929+
const dt = helperDateService.create();
930+
const start = helperDateService.startOfYear(dt);
931+
const end = helperDateService.endOfYear(dt);
932+
933+
expect(result).toBeTruthy();
934+
expect(`${result.startDate}`).toBe(`${start}`);
935+
expect(`${result.endDate}`).toBe(`${end}`);
936+
});
937+
938+
it('should be success options year', async () => {
939+
const result: IHelperDateStartAndEndDate =
940+
helperDateService.getStartAndEndDate({ year: 2022 });
941+
942+
jest.spyOn(
943+
helperDateService,
944+
'getStartAndEndDate'
945+
).mockReturnValueOnce(result);
946+
947+
const dt = helperDateService.create('2022-01-02');
948+
const start = helperDateService.startOfYear(dt);
949+
const end = helperDateService.endOfYear(dt);
950+
951+
expect(result).toBeTruthy();
952+
expect(`${result.startDate}`).toBe(`${start}`);
953+
expect(`${result.endDate}`).toBe(`${end}`);
954+
});
955+
956+
it('should be success options year and month', async () => {
957+
const result: IHelperDateStartAndEndDate =
958+
helperDateService.getStartAndEndDate({ year: 2022, month: 2 });
959+
960+
jest.spyOn(
961+
helperDateService,
962+
'getStartAndEndDate'
963+
).mockReturnValueOnce(result);
964+
965+
const dt = helperDateService.create('2022-02-02');
966+
const start = helperDateService.startOfMonth(dt);
967+
const end = helperDateService.endOfMonth(dt);
968+
969+
expect(result).toBeTruthy();
970+
expect(`${result.startDate}`).toBe(`${start}`);
971+
expect(`${result.endDate}`).toBe(`${end}`);
972+
});
973+
});
915974
});

0 commit comments

Comments
 (0)