Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/support timezone setup in minute #65

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
modulePaths: ['./'],
preset: "ts-jest",
testEnvironment: "node",
modulePaths: ["./"],
setupFiles: ["<rootDir>/test/dotenv-config.js"],
};
7 changes: 4 additions & 3 deletions src/modules/partner-config/dto/partner-update-info.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsNotEmpty, IsNumber, IsOptional, IsString } from "class-validator";
import { IsInt, IsNotEmpty, IsOptional, IsString } from "class-validator";
import { Type } from "class-transformer";

export class PartnerUpdateInfoDto {
Expand All @@ -22,12 +22,13 @@ export class PartnerUpdateInfoDto {
companyDescription: string;

@Type(() => Number)
@IsInt({ message: "Value must be an integer" })
@IsNotEmpty()
@ApiProperty({
description:
"Company timezone -> Job calculate worker salary start from new day at 00:00:00 - 00:00:30 (server_timestamp + company timezone setup)",
"Noted: Timezone save on backend must be in minutes.Company timezone -> Job calculate worker salary start from new day at 00:00:00 - 00:00:30 (server_timestamp + company timezone setup)",
required: true,
example: 7,
example: 420,
})
timezone: number;
}
10 changes: 10 additions & 0 deletions src/shares/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@ export function generateRandomString(): string {

return string;
}

// Random from 0 -> 24
export function randomHour(): number {
return Math.floor(Math.random() * 24);
}

// Random from 0 -> 60
export function randomMinute(): number {
return Math.floor(Math.random() * 60);
}
36 changes: 24 additions & 12 deletions src/tasks/init-salary-job.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
JobStatus,
JobType,
} from "src/models/entities/job.entity";
import { DayInMinutes, HourInMinutes } from "src/tasks/task.const";
import { CompanyInfoEntity } from "src/models/entities/company_info.entity";
import { CompanyInfoRepository } from "src/models/repositories/company_info.repository";

Expand Down Expand Up @@ -38,21 +39,28 @@ export class InitSalaryJobTask {
* ...
*
* @param currentTimeIn0UTCInHour
* @param currentTimeIn0UTCInMinute
*/
public getTimezoneNeedToCalculate(currentTimeIn0UTCInHour: number): number[] {
if (currentTimeIn0UTCInHour === 0) return [0];
// Only need to get company with negative time zone
if (currentTimeIn0UTCInHour >= 0 && currentTimeIn0UTCInHour <= 9) {
return [-currentTimeIn0UTCInHour];
public getTimezoneNeedToCalculate(
currentTimeIn0UTCInHour: number,
currentTimeIn0UTCInMinute: number
): number[] {
const timeDiffInMinute =
currentTimeIn0UTCInHour * HourInMinutes + currentTimeIn0UTCInMinute;

if (timeDiffInMinute === 0) return [0];
// Only need to get company with negative time zone (from 0 to 9 hour) convert to minutes
if (timeDiffInMinute >= 0 && timeDiffInMinute <= 540) {
return [-timeDiffInMinute];
}

// Need to get both company with negative time zone and positive time zone
if (currentTimeIn0UTCInHour >= 10 && currentTimeIn0UTCInHour <= 12) {
return [-currentTimeIn0UTCInHour, 24 - currentTimeIn0UTCInHour];
// Need to get both company with negative time zone and positive time zone (from 10 to 12)
if (timeDiffInMinute >= 600 && timeDiffInMinute <= 720) {
return [-timeDiffInMinute, DayInMinutes - timeDiffInMinute];
}

// Only need to get company with positive time zone
return [24 - currentTimeIn0UTCInHour];
return [DayInMinutes - timeDiffInMinute];
}
/**
* @description: With old logic, we select all company each time this schedule run, then we add their timezone to
Expand All @@ -67,11 +75,15 @@ export class InitSalaryJobTask {
currentServerTime: moment.Moment
): Promise<CompanyInfoEntity[]> {
const currentTimeIn0UTCInHour = currentServerTime.utc().hour();
const currentTimeIn0UTCInMinute = currentServerTime.utc().minute();
const listTimezoneNeedToCalculate = this.getTimezoneNeedToCalculate(
currentTimeIn0UTCInHour
currentTimeIn0UTCInHour,
currentTimeIn0UTCInMinute
);
this.logger.log(
`Current server time in UTC0 is ${currentTimeIn0UTCInHour}. Finding companies with timezone ${listTimezoneNeedToCalculate}`
`Current server time in UTC0 is ${
currentTimeIn0UTCInHour * HourInMinutes + currentTimeIn0UTCInMinute
}. Finding companies with timezone ${listTimezoneNeedToCalculate}`
);
return await this.companyInfoRepository.getAllCompanyWithTimezones(
listTimezoneNeedToCalculate
Expand All @@ -96,7 +108,7 @@ export class InitSalaryJobTask {
for (const companyInfo of listCompany) {
const currentCompanyDateTime = currentServerTime
.utc() // Get current time at UTC +0
.add(companyInfo.timezone, "hours") // Add company timezone to get current company time
.add(companyInfo.timezone, "minutes") // Add company timezone to get current company time
.set({ hour: 0, minute: 0, second: 0, millisecond: 0 }) // Round hour, minute and second to 0
.subtract(Number(process.env.CALCULATE_SALARY_AFTER_DAY), "days") // Calculate salary after day
.format("YYYY-MM-DD");
Expand Down
3 changes: 3 additions & 0 deletions src/tasks/task.const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const HourInMinutes = 60;
export const DayInMinutes = 1440;
export const DayInHours = 24;
1 change: 1 addition & 0 deletions test/e2e/partner-config/partner-config.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe("Partner Config Service", () => {
const mockPartnerUpdateInfoDto = new PartnerUpdateInfoDto();
mockPartnerUpdateInfoDto.companyName = "mock company name";
mockPartnerUpdateInfoDto.companyDescription = "mock description";
mockPartnerUpdateInfoDto.timezone = 1500;

it("Should be forbidden because not login", async () => {
const response = await request(app.getHttpServer())
Expand Down
2 changes: 1 addition & 1 deletion test/unit/partner-config/partner-config.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("Partner Config Service", () => {
});

describe("Test Update Company Info", () => {
const mockTimezone = 7;
const mockTimezone = 1500;
it("Create new company info because company info not existed", async () => {
const mockCompanyName = "testCompanyName";
const mockCompanyDescription = "testDescription";
Expand Down
Loading
Loading