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

Maryia/webrel-483/TS migration of ContractType function #17

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: 4 additions & 2 deletions packages/shared/src/utils/helpers/__tests__/start-date.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ContractsFor } from '@deriv/api-types';
import { buildForwardStartingConfig } from '../start-date';

describe('start_date', () => {
describe('buildForwardStartingConfig', () => {
it('Returns empty object when forward_starting_options and forward_starting_dates are both empties', () => {
const contract = {
const contract: ContractsFor['available'][number] = {
barrier_category: 'euro_atm',
barriers: 0,
contract_category: 'callput',
Expand All @@ -19,7 +20,8 @@ describe('start_date', () => {
start_type: 'spot',
submarket: 'major_pairs',
underlying_symbol: 'frxAUDJPY',
forward_starting_options: [],
forward_starting_options:
[] as unknown as ContractsFor['available'][number]['forward_starting_options'],
};
/* eslint-disable no-unused-expressions */
expect(buildForwardStartingConfig(contract, [])).toHaveLength(0);
Expand Down
23 changes: 8 additions & 15 deletions packages/shared/src/utils/helpers/start-date.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import moment from 'moment';
import { toMoment } from '../date';

type TForwardStartingDates = {
blackouts?: unknown[];
close?: string;
date: string;
open?: string;
};

type TContract = {
forward_starting_options: TForwardStartingDates[];
};
import { ContractsFor } from '@deriv/api-types';

type TConfig = {
text: string;
Expand All @@ -21,19 +11,22 @@ type TConfig = {
}[];
}[];

export const buildForwardStartingConfig = (contract: TContract, forward_starting_dates: TForwardStartingDates[]) => {
export const buildForwardStartingConfig = (
contract: ContractsFor['available'][number],
forward_starting_dates?: TConfig
) => {
const forward_starting_config: TConfig = [];

if ((contract.forward_starting_options || []).length) {
contract.forward_starting_options.forEach(option => {
const duplicated_option = forward_starting_config.find(opt => opt.value === parseInt(option.date));
(contract.forward_starting_options ?? []).forEach(option => {
const duplicated_option = forward_starting_config.find(opt => opt.value === parseInt(option.date ?? ''));
const current_session = { open: toMoment(option.open), close: toMoment(option.close) };
if (duplicated_option) {
duplicated_option.sessions.push(current_session);
} else {
forward_starting_config.push({
text: toMoment(option.date).format('ddd - DD MMM, YYYY'),
value: parseInt(option.date),
value: parseInt(option.date ?? ''),
sessions: [current_session],
});
}
Expand Down
Loading