2022-12-20 20:14:56 +00:00
|
|
|
import type { Privilege } from "@/types/models/privilege.type";
|
|
|
|
import type { GetAllPrivilegiesFormat, RawPrivilege } from "./types";
|
|
|
|
|
|
|
|
export const validatePrivilege = (privilege: RawPrivilege): Error | null => {
|
|
|
|
const typeValues: typeof privilege.type[] = ["count", "day", "full"];
|
|
|
|
|
|
|
|
if (!typeValues.includes(privilege.type)) {
|
2022-12-24 12:18:28 +00:00
|
|
|
return new Error("invalid <type> value");
|
2022-12-20 20:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isNaN(Number(privilege.price))) {
|
|
|
|
return new Error("price must be a number");
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-12-22 10:45:44 +00:00
|
|
|
export const defineGetAllPrivilegiesFormat = (query?: string): GetAllPrivilegiesFormat => {
|
|
|
|
if (!query) {
|
|
|
|
return "array";
|
|
|
|
}
|
|
|
|
|
2022-12-20 20:14:56 +00:00
|
|
|
const formats: GetAllPrivilegiesFormat[] = ["array", "map"];
|
|
|
|
const findedFormat = formats.find((format) => query === format);
|
|
|
|
|
|
|
|
if (!findedFormat) {
|
|
|
|
return "array";
|
|
|
|
}
|
|
|
|
|
|
|
|
return findedFormat;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const convertPrivilegiesToMap = (privilegies: Privilege[]) => {
|
|
|
|
return privilegies.reduce<Record<string, Privilege[]>>((accamulator, privilege) => {
|
|
|
|
if (!accamulator[privilege.serviceKey]) {
|
|
|
|
accamulator[privilege.serviceKey] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
accamulator[privilege.serviceKey].push(privilege);
|
|
|
|
|
|
|
|
return accamulator;
|
|
|
|
}, {});
|
|
|
|
};
|