106 lines
2.9 KiB
TypeScript
Executable File
106 lines
2.9 KiB
TypeScript
Executable File
import type {
|
|
BookingEntry,
|
|
BookingInputValues,
|
|
ServerError,
|
|
} from "../data.types";
|
|
import { getAllMatchingChildNumbersObject } from "../finalsheet.utils";
|
|
import { permutations } from "../permutations";
|
|
import { getULID } from "..";
|
|
import { parseToDateString } from "../datetime.helper.utils";
|
|
|
|
export function getParsedBookingEntries(
|
|
values: BookingInputValues,
|
|
chosenLexiCodes: string[],
|
|
isPossibleCombinationMode: boolean,
|
|
drawId: number
|
|
) {
|
|
const MAX_NO_LEN = 4;
|
|
const inputNumbers = values.number.split(".");
|
|
const out = [] as BookingEntry[];
|
|
const today = new Date();
|
|
// INFO: these 2 IDs would be modified during the algorithm run anyways, so this is okay
|
|
const commonInfo = {
|
|
distributorId: 0,
|
|
dealerId: 0,
|
|
drawId,
|
|
changedBalance: 0,
|
|
bookDate: parseToDateString(today),
|
|
};
|
|
if (chosenLexiCodes.length > 0) {
|
|
for (const number of inputNumbers) {
|
|
let __numbers = new Set<string>();
|
|
__numbers.add(number);
|
|
if (isPossibleCombinationMode) {
|
|
__numbers = findAllNumberPossibleCombinations(number, MAX_NO_LEN);
|
|
}
|
|
for (const each of __numbers) {
|
|
const children = getAllMatchingChildNumbersObject(each);
|
|
for (const lc of chosenLexiCodes) {
|
|
const no = children[lc as keyof typeof children];
|
|
const id = getULID();
|
|
out.push({
|
|
id,
|
|
number: no,
|
|
first: parseInt(values.first[lc]),
|
|
second: parseInt(values.second[lc]),
|
|
requestId: id,
|
|
...commonInfo,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
for (const number of inputNumbers) {
|
|
const id = getULID();
|
|
out.push({
|
|
id,
|
|
number,
|
|
first: parseInt(values.default.first),
|
|
second: parseInt(values.default.second),
|
|
requestId: id,
|
|
...commonInfo,
|
|
});
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function findAllNumberPossibleCombinations(nombor: string, maxLength: number) {
|
|
const out = new Set<string>();
|
|
for (const each_perm of permutations(nombor.split(""), maxLength)) {
|
|
out.add(each_perm.join(""));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function ensureInputIsValid(
|
|
values: BookingInputValues,
|
|
chosenLexiCodes: string[]
|
|
) {
|
|
const errors = [] as ServerError;
|
|
const rates = [
|
|
...Object.values(values.default),
|
|
...Object.values(values.first),
|
|
...Object.values(values.second),
|
|
];
|
|
for (const each of rates) {
|
|
if (each.length < 1) {
|
|
continue;
|
|
}
|
|
if (parseInt(each) % 5 !== 0) {
|
|
errors.push({ message: `${each} rate must be a multiple of 5` });
|
|
}
|
|
}
|
|
for (const lexiCode of chosenLexiCodes) {
|
|
const lexiCodeMinLen = lexiCode.replaceAll("+", "").length;
|
|
for (const number of values.number.split(".")) {
|
|
if (number.length < lexiCodeMinLen) {
|
|
errors.push({
|
|
message: `${number} must be at least ${lexiCodeMinLen} digits to book ${lexiCode}`,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return errors;
|
|
}
|