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

Migrate payments and Transactions to typescript #9896

Merged
merged 8 commits into from
Apr 24, 2024
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: 1 addition & 1 deletion scripts/ledger/refund-host-fee-on-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const main = async () => {
`Refunding host fee for transaction ${transactionGroup} (${transaction.description}) on behalf of ${userCollectiveSlug}`,
);
if (!DRY_RUN) {
await refundHostFee(transaction, user, 0, uuid(), null);
await refundHostFee(transaction, user, 0, uuid());
}
};

Expand Down
7 changes: 1 addition & 6 deletions scripts/ledger/split-host-fee-shares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,9 @@ const migrate = async () => {
console.log(`Migrated ${count}/${hostFeeTransactions.length} transactions`);
}

const host = await models.Collective.findByPk(hostFeeTransaction.CollectiveId, { paranoid: false });
const transaction = await hostFeeTransaction.getRelatedTransaction({ kind: ['CONTRIBUTION', 'ADDED_FUNDS'] });
// TODO preload Payment method to define wether debts have ben created automatically
const result = await models.Transaction.createHostFeeShareTransactions(
{ transaction, hostFeeTransaction },
host,
false,
);
const result = await models.Transaction.createHostFeeShareTransactions({ transaction, hostFeeTransaction });

results.push(Boolean(result));
}
Expand Down
12 changes: 1 addition & 11 deletions scripts/ledger/split-host-fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const migrate = async () => {
const groupedTransactions = Object.values(groupBy(transactions, 'TransactionGroup'));
const timestamp = Date.now().toString();
const transactionsData = { hostFeeMigration: timestamp };
const hostsCache = {};
let count = 0;

console.log(`Migrating ${groupedTransactions.length} transaction pairs...`);
Expand All @@ -64,18 +63,9 @@ const migrate = async () => {
continue;
}

// Caching hosts (small optimization)
let host;
if (credit.HostCollectiveId && hostsCache[credit.HostCollectiveId]) {
host = hostsCache[credit.HostCollectiveId];
} else {
host = await credit.getHostCollective();
hostsCache[host.id] = host;
}

// Create host fee transaction
const creditPreMigrationData = pick(credit.dataValues, BACKUP_COLUMNS);
const result = await models.Transaction.createHostFeeTransactions(credit, host, transactionsData);
const result = await models.Transaction.createHostFeeTransactions(credit, transactionsData);
if (!result) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/ledger/update-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const MODIFIERS = {
const host = accountsCache[transaction.HostCollectiveId] || (await transaction.getHostCollective());
accountsCache[transaction.HostCollectiveId] = host;
transaction.hostFeeInHostCurrency = transaction.amount * (percentage / 100);
await models.Transaction.createHostFeeTransactions(transaction, host);
await models.Transaction.createHostFeeTransactions(transaction);
}
},
},
Expand Down
3 changes: 2 additions & 1 deletion scripts/taxes/add-tax-on-expense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Command } from 'commander';
import { sum } from 'lodash';

import models, { sequelize } from '../../server/models';
import { ExpenseTaxDefinition } from '../../server/models/Expense';

const program = new Command()
.description('Helper to add a tax on an existing expense')
Expand Down Expand Up @@ -44,7 +45,7 @@ const main = async () => {
}
}

const tax = { type: 'GST', id: 'GST', rate: 0.15, percentage: 15 };
const tax = { type: 'GST', id: 'GST', rate: 0.15, percentage: 15 } as ExpenseTaxDefinition;
const taxAmountFromExpense = getTaxAmount(expense.amount, tax.rate);
const taxAmountFromItems = sum(expense.items.map(i => getTaxAmount(i.amount, tax.rate)));
if (taxAmountFromExpense !== taxAmountFromItems) {
Expand Down
1 change: 1 addition & 0 deletions server/constants/paymentMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum PAYMENT_METHOD_SERVICE {
export const PAYMENT_METHOD_SERVICES = Object.values(PAYMENT_METHOD_SERVICE);

export enum PAYMENT_METHOD_TYPE {
DEFAULT = 'default',
ALIPAY = 'alipay',
CREDITCARD = 'creditcard',
PREPAID = 'prepaid',
Expand Down
12 changes: 3 additions & 9 deletions server/graphql/common/expenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import { canUseFeature } from '../../lib/user-permissions';
import { formatCurrency, parseToBoolean } from '../../lib/utils';
import models, { Collective, sequelize } from '../../models';
import AccountingCategory from '../../models/AccountingCategory';
import Expense, { ExpenseDataValuesByRole, ExpenseStatus } from '../../models/Expense';
import Expense, { ExpenseDataValuesByRole, ExpenseStatus, ExpenseTaxDefinition } from '../../models/Expense';
import ExpenseAttachedFile from '../../models/ExpenseAttachedFile';
import ExpenseItem from '../../models/ExpenseItem';
import LegalDocument, { LEGAL_DOCUMENT_TYPE } from '../../models/LegalDocument';
Expand Down Expand Up @@ -1141,12 +1141,6 @@ export const hasMultiCurrency = (collective, host): boolean => {
return collective.currency === host?.currency; // Only support multi-currency when collective/host have the same currency
};

type TaxDefinition = {
type: string;
rate: number;
idNumber: string;
};

type ExpenseData = {
id?: number;
payoutMethod?: Record<string, unknown>;
Expand All @@ -1164,7 +1158,7 @@ type ExpenseData = {
longDescription?: string;
amount?: number;
currency?: SupportedCurrency;
tax?: TaxDefinition[];
tax?: ExpenseTaxDefinition[];
customData: Record<string, unknown>;
accountingCategory?: AccountingCategory;
};
Expand Down Expand Up @@ -1934,7 +1928,7 @@ export async function editExpense(req: express.Request, expenseData: ExpenseData
const updatedItemsData: Partial<ExpenseItem>[] =
(await prepareExpenseItemInputs(expenseCurrency, expenseData.items, { isEditing: true })) || expense.items;
const [hasItemChanges, itemsDiff] = await getItemsChanges(expense.items, updatedItemsData);
const taxes = expenseData.tax || (expense.data?.taxes as TaxDefinition[]) || [];
const taxes = expenseData.tax || (expense.data?.taxes as ExpenseTaxDefinition[]) || [];
checkTaxes(expense.collective, expense.collective.host, expenseType, taxes);

if (!options?.['skipPermissionCheck'] && !(await canEditExpense(req, expense))) {
Expand Down
2 changes: 1 addition & 1 deletion server/graphql/v1/mutations/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export async function createOrder(order, req) {
orderPublicData = pick(order.data, []);
}

const platformTipEligible = await libPayments.isPlatformTipEligible({ ...order, collective }, host);
const platformTipEligible = await libPayments.isPlatformTipEligible({ ...order, collective });

const orderData = {
CreatedByUserId: remoteUser.id,
Expand Down
2 changes: 1 addition & 1 deletion server/graphql/v1/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ export const OrderType = new GraphQLObjectType({
type: GraphQLJSON,
description: 'Additional information on order: tax and custom fields',
resolve(order) {
return pick(order.data, ['tax', 'customData', 'isFeesOnTop', 'platformFee', 'hasPlatformTip', 'platformTip']);
return pick(order.data, ['tax', 'customData', 'platformFee', 'hasPlatformTip', 'platformTip']);
},
},
stripeError: {
Expand Down
2 changes: 1 addition & 1 deletion server/lib/budget.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export async function getSumCollectivesAmountReceived(

export async function getTotalMoneyManagedAmount(
host,
{ endDate, collectiveIds, currency, version, loaders = null } = {},
{ endDate, collectiveIds, currency, version = null, loaders = null } = {},
) {
version = version || host.settings?.budget?.version || DEFAULT_BUDGET_VERSION;
currency = currency || host.currency;
Expand Down
Loading
Loading