Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 2x 2x 2x 2x 2x 20x 20x 16x 16x 16x 20x 20x 2x 2x 2x 20x 20x 20x 20x 1x 1x 98x 98x 98x 98x 98x 98x 98x 94x 98x 3x 3x 3x 1x 1x 98x 98x 1x 98x 98x 1x 1x 1x 1x 1x 1x 1x 98x 98x 98x 98x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 6x 6x 6x 72x 72x 72x 72x 72x 72x 72x 72x 72x 6x 6x 20x 20x 4x 4x 4x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 4x 4x 20x 20x 2x 2x 2x 2x 2x 2x 2x 20x 20x 8x 8x 8x 8x 8x 8x 8x 20x 20x 20x 20x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 80x 80x 80x 20x 1x | import { times } from 'lodash-es';
import { VariableType, type Quota } from '@amalia/amalia-lang/tokens/types';
import { type UpsertQuotaAssignmentRequest } from '@amalia/assignments/quotas/types';
import { FormatsEnum } from '@amalia/data-capture/fields/types';
import { convertYYYYMMDDToUtcTimestamp } from '@amalia/ext/dates';
import { dayjs } from '@amalia/ext/dayjs';
import { type CurrencySymbolsEnum } from '@amalia/ext/iso-4217';
import { PeriodFrequencyEnum } from '@amalia/payout-definition/periods/types';
import { type Plan } from '@amalia/payout-definition/plans/types';
import { type TeamContract } from '@amalia/tenants/teams/types';
import { type UserProfile } from '@amalia/tenants/users/profile/types';
import { QUOTA_FREQUENCY_COLUMNS } from './quotas-csv.constants';
import {
type CsvPlanQuotaRow,
type CsvQuotaMonthlyRow,
type CsvQuotaQuarterlyRow,
type CsvQuotaRow,
type CsvQuotaYearlyRow,
type CsvTeamQuotaRow,
type CsvUserQuotaRow,
} from './quotas-csv.types';
/**
* Based on quota type, will try to match a plan/team or user from the csv row.
* If it finds a match, it will return the attributes to be used in the upsert request.
* For users, it will also set the currency if the quota is currency based.
* If no match is found, it will return null.
*/
const getAttributesFromQuotaType = ({
csvQuotaRow,
quota,
teams,
plans,
users,
defaultCurrency,
}: {
csvQuotaRow: CsvQuotaRow;
quota: Pick<Quota, 'format' | 'type'>;
teams?: Pick<TeamContract, 'id' | 'name'>[];
plans?: Pick<Plan, 'id' | 'name'>[];
users?: Pick<UserProfile, 'currency' | 'email' | 'id'>[];
defaultCurrency: CurrencySymbolsEnum;
}): Pick<UpsertQuotaAssignmentRequest, 'currency' | 'planId' | 'teamId' | 'userId'> | null => {
const isCurrencyQuota = quota.format === FormatsEnum.currency;
const currency = isCurrencyQuota ? defaultCurrency : undefined;
switch (quota.type) {
case VariableType.user: {
const user = users?.find(
(user) => user.email.toLowerCase() === (csvQuotaRow as CsvUserQuotaRow).email?.toLowerCase(),
);
return user ? { userId: user.id, currency: isCurrencyQuota ? user.currency : undefined } : null;
}
case VariableType.team: {
const team = teams?.find((team) => team.name === (csvQuotaRow as CsvTeamQuotaRow).name);
return team ? { teamId: team.id, currency } : null;
}
case VariableType.plan: {
const plan = plans?.find((plan) => plan.name === (csvQuotaRow as CsvPlanQuotaRow).name);
return plan ? { planId: plan.id, currency } : null;
}
default:
return null;
}
};
const transformCsvValueToQuotaAssignmentValue = (
value: string | undefined,
format: FormatsEnum,
): Partial<UpsertQuotaAssignmentRequest>['value'] => {
switch (format) {
case FormatsEnum.currency:
case FormatsEnum.number:
case FormatsEnum.percent:
return value ? Number.parseFloat(value) : undefined;
case FormatsEnum.date:
try {
return value ? convertYYYYMMDDToUtcTimestamp(value) : undefined;
} catch {
return undefined;
}
case FormatsEnum.text:
default:
return value;
}
};
type MappedQuotaAssignment = Pick<UpsertQuotaAssignmentRequest, 'endDate' | 'startDate'> & {
value?: UpsertQuotaAssignmentRequest['value'] | null;
};
const filterEmptyQuotaAssignment = (
quotaAssignment: MappedQuotaAssignment,
): quotaAssignment is Pick<UpsertQuotaAssignmentRequest, 'endDate' | 'startDate' | 'value'> =>
quotaAssignment.value !== undefined &&
quotaAssignment.value !== null &&
quotaAssignment.value !== '' &&
!Number.isNaN(quotaAssignment.value);
/**
* Based on the quota frequency, will return the upsert requests for each period.
* Empty cells are ignored.
*/
const getUpsertQuotaAssignmentRequestsFromQuotaFrequency = ({
csvQuotaRow,
quota,
year,
}: {
csvQuotaRow: CsvQuotaRow;
quota: Pick<Quota, 'format' | 'frequency'>;
year: number;
}): Pick<UpsertQuotaAssignmentRequest, 'endDate' | 'startDate' | 'value'>[] => {
switch (quota.frequency) {
case PeriodFrequencyEnum.month:
return times(12)
.map(
(month): MappedQuotaAssignment => ({
startDate: dayjs.utc({ year, month }).startOf('month').unix(),
endDate: dayjs.utc({ year, month }).endOf('month').unix(),
value: transformCsvValueToQuotaAssignmentValue(
(csvQuotaRow as CsvQuotaMonthlyRow)[
QUOTA_FREQUENCY_COLUMNS[PeriodFrequencyEnum.month][month].toLowerCase() as keyof CsvQuotaMonthlyRow
],
quota.format,
),
}),
)
.filter(filterEmptyQuotaAssignment);
case PeriodFrequencyEnum.quarter:
return times(4)
.map(
(quarter): MappedQuotaAssignment => ({
startDate: dayjs
.utc({ year })
.quarter(quarter + 1)
.startOf('quarter')
.unix(),
endDate: dayjs
.utc({ year })
.quarter(quarter + 1)
.endOf('quarter')
.unix(),
value: transformCsvValueToQuotaAssignmentValue(
(csvQuotaRow as CsvQuotaQuarterlyRow)[
QUOTA_FREQUENCY_COLUMNS[PeriodFrequencyEnum.quarter][
quarter
].toLowerCase() as keyof CsvQuotaQuarterlyRow
],
quota.format,
),
}),
)
.filter(filterEmptyQuotaAssignment);
case PeriodFrequencyEnum.year:
return [
{
startDate: dayjs.utc({ year }).startOf('year').unix(),
endDate: dayjs.utc({ year }).endOf('year').unix(),
value: transformCsvValueToQuotaAssignmentValue((csvQuotaRow as CsvQuotaYearlyRow).value, quota.format),
} satisfies MappedQuotaAssignment as MappedQuotaAssignment,
].filter(filterEmptyQuotaAssignment);
case PeriodFrequencyEnum.null:
return [
{
startDate: null,
endDate: null,
value: transformCsvValueToQuotaAssignmentValue((csvQuotaRow as CsvQuotaYearlyRow).value, quota.format),
} satisfies MappedQuotaAssignment as MappedQuotaAssignment,
].filter(filterEmptyQuotaAssignment);
default:
return [];
}
};
/**
* Transforms the csv rows into upsert requests for quota assignments.
*/
export const transformCsvQuotaRowsToUpsertQuotaAssignmentRequests = ({
csvQuotaRows,
quota,
teams,
plans,
users,
year,
defaultCurrency,
}: {
/** Rows from the CSV. */
csvQuotaRows: CsvQuotaRow[];
/** Current quota. All rows are based on this quota. */
quota: Pick<Quota, 'format' | 'frequency' | 'id' | 'type'>;
/** List of teams for the organization. If team quota, will match on name. */
teams?: Pick<TeamContract, 'id' | 'name'>[];
/** List of plans for the organization. If plan quota, will match on name. */
plans?: Pick<Plan, 'id' | 'name'>[];
/** List of users for the organization. If user quota, will match on email. */
users?: Pick<UserProfile, 'currency' | 'email' | 'id'>[];
/** Selected year for the csv file. */
year: number;
/** Currency to use for team and plan quotas if the quota format is currency. */
defaultCurrency: CurrencySymbolsEnum;
}) =>
csvQuotaRows.flatMap((csvQuotaRow) => {
const attributesFromQuotaType = getAttributesFromQuotaType({
csvQuotaRow,
quota,
teams,
plans,
users,
defaultCurrency,
});
if (!attributesFromQuotaType) {
return [];
}
return getUpsertQuotaAssignmentRequestsFromQuotaFrequency({
csvQuotaRow,
quota,
year,
}).map((quotaAssignment) => ({
...quotaAssignment,
...attributesFromQuotaType,
variableId: quota.id,
}));
});
|