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: move duration menstrual subscription to env #231

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
2 changes: 2 additions & 0 deletions src/common/secrets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export const SecretKeyList = {
LAB_ORDER_LINK: process.env.LAB_ORDER_LINK || null,
UNSTAKE_TIMER: process.env.UNSTAKE_TIMER || null,
UNSTAKE_INTERVAL: process.env.UNSTAKE_INTERVAL || null,
MENSTRUAL_SUBSCRIPTION_DURATION:
process.env.MENSTRUAL_SUBSCRIPTION_DURATION || null,
} as const;

export type keyList = keyof typeof SecretKeyList;
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class MenstrualSubscriptionService {
private logger: Logger = new Logger(MenstrualSubscriptionService.name);
private isRunning = false;
private timer: number;
private menstrualSubscriptionDuration: { [key: string]: number };
constructor(
private readonly gCloudSecretManagerService: GCloudSecretManagerService<keyList>,
private readonly elasticsearchService: ElasticsearchService,
Expand All @@ -28,6 +29,9 @@ export class MenstrualSubscriptionService {
this.gCloudSecretManagerService.getSecret('UNSTAKE_INTERVAL').toString(),
);

this.menstrualSubscriptionDuration =
this.parseMenstrualSubscriptionDuration();

const menstrualSubscription = setInterval(async () => {
await this.handleWaitingUnstaked();
}, unstakeInterval);
Expand Down Expand Up @@ -111,18 +115,45 @@ export class MenstrualSubscriptionService {
}
}

private milisecondsConverter = {
Monthly: 30 * 24 * 60 * 60 * 1000,
Quarterly: 3 * 30 * 24 * 60 * 60 * 1000,
Yearly: 12 * 30 * 24 * 60 * 60 * 1000,
};

checkTimeDurationEnd(
currtime: number,
date: number,
duration: string,
): boolean {
return currtime >= date + this.milisecondsConverter[duration];
return currtime >= date + this.menstrualSubscriptionDuration[duration];
}

parseMenstrualSubscriptionDuration(): { [key: string]: number } {
try {
const menstrualSubscriptionDurationObj: { [key: string]: string } =
JSON.parse(
this.gCloudSecretManagerService
.getSecret('MENSTRUAL_SUBSCRIPTION_DURATION')
.toString() ?? '{}',
);
const parseMenstrualSubscriptionDuration: Map<string, number> = new Map<
string,
number
>();

Object.entries(menstrualSubscriptionDurationObj).forEach(
([key, value]) => {
parseMenstrualSubscriptionDuration.set(
key,
this.strToMilisecond(value),
);
},
);

console.log(Object.fromEntries(parseMenstrualSubscriptionDuration));
return Object.fromEntries(parseMenstrualSubscriptionDuration);
} catch (error) {
console.log(error);
this.logger.log(
`parse menstrual subscription env error: ${error.message}`,
);
return {};
}
}

strToMilisecond(timeFormat: string): number {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { SubstrateService } from '@common/substrate';
import { GCloudSecretManagerService } from '@debionetwork/nestjs-gcloud-secret-manager';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { SchedulerRegistry } from '@nestjs/schedule';
import { Test, TestingModule } from '@nestjs/testing';
import { MenstrualSubscriptionService } from '@schedulers/menstrual-subscription/menstrual-subscription.service';
import {
elasticsearchServiceMockFactory,
MockLogger,
MockType,
schedulerRegistryMockFactory,
substrateServiceMockFactory,
} from '../../mock';

describe('Menstrual Subscription Service', () => {
let menstrualSubscriptionService: MockType<MenstrualSubscriptionService>;
let elasticsearchServiceMock: MockType<ElasticsearchService>;
let substrateServiceMock: MockType<SubstrateService>;
let schedulerRegistryMock: MockType<SchedulerRegistry>;
const strToMilisecondSpy = jest.spyOn(
MenstrualSubscriptionService.prototype,
'strToMilisecond',
);

const MENSTRUAL_SUBSCRIPTION_DURATION =
'{"Monthly": "30:00:00:00", "Quarterly": "90:00:00:00", "Yearly": "365:00:00:00"}';
const INTERVAL = '00:00:00:30';
const TIMER = '6:00:00:00';

class GoogleSecretManagerServiceMock {
_secretsList = new Map<string, string>([
['MENSTRUAL_SUBSCRIPTION_DURATION', MENSTRUAL_SUBSCRIPTION_DURATION],
['UNSTAKE_INTERVAL', INTERVAL],
['UNSTAKE_TIMER', TIMER],
]);
loadSecrets() {
return null;
}

getSecret(key) {
return this._secretsList.get(key);
}
}

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
MenstrualSubscriptionService,
{
provide: GCloudSecretManagerService,
useClass: GoogleSecretManagerServiceMock,
},
{
provide: ElasticsearchService,
useFactory: elasticsearchServiceMockFactory,
},
{
provide: SubstrateService,
useFactory: substrateServiceMockFactory,
},
{
provide: SchedulerRegistry,
useFactory: schedulerRegistryMockFactory,
},
],
}).compile();
module.useLogger(MockLogger);

menstrualSubscriptionService = module.get(MenstrualSubscriptionService);
elasticsearchServiceMock = module.get(ElasticsearchService);
substrateServiceMock = module.get(SubstrateService);
schedulerRegistryMock = module.get(SchedulerRegistry);
});

afterAll(() => {
schedulerRegistryMock = null;
});

it('should return 30 days in milisecond', () => {
const PARAM = '06:00:00:00';
const EXPECTED_RETURN = 6 * 24 * 60 * 60 * 1000;

expect(menstrualSubscriptionService.strToMilisecond(PARAM)).toBe(
EXPECTED_RETURN,
);
});

it('should parse mesntrual subscription duration env value', () => {
expect(
menstrualSubscriptionService.parseMenstrualSubscriptionDuration(),
).toEqual(
expect.objectContaining({
Monthly: 30 * 24 * 60 * 60 * 1000,
Quarterly: 3 * 30 * 24 * 60 * 60 * 1000,
Yearly: 365 * 24 * 60 * 60 * 1000,
}),
);
});
});