Skip to content

Commit

Permalink
Merge pull request #128 from letehaha/fix/remove-sequelize-explicit-t…
Browse files Browse the repository at this point in the history
…x-usages

fix: Remove sequelize tx useless explicit usages
  • Loading branch information
letehaha committed Sep 14, 2024
2 parents cf1c749 + aac1d83 commit 0471ab9
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 312 deletions.
13 changes: 2 additions & 11 deletions src/models/Categories.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Transaction } from 'sequelize/types';
import { CATEGORY_TYPES } from 'shared-types';
import { Table, Column, Model, ForeignKey, DataType, BelongsToMany } from 'sequelize-typescript';
import Users from './Users.model';
Expand Down Expand Up @@ -102,10 +101,7 @@ export interface EditCategoryPayload {
color?: string;
}

export const editCategory = async (
{ userId, categoryId, ...params }: EditCategoryPayload,
{ transaction }: { transaction?: Transaction } = {},
) => {
export const editCategory = async ({ userId, categoryId, ...params }: EditCategoryPayload) => {
const existingCategory = await Categories.findByPk(categoryId);
if (!existingCategory) {
throw new NotFoundError({ message: 'Category with provided id does not exist!' });
Expand All @@ -116,7 +112,6 @@ export const editCategory = async (
userId,
},
returning: true,
transaction,
});

return categories;
Expand All @@ -127,13 +122,9 @@ export interface DeleteCategoryPayload {
categoryId: number;
}

export const deleteCategory = async (
{ userId, categoryId }: DeleteCategoryPayload,
{ transaction }: { transaction?: Transaction } = {},
) => {
export const deleteCategory = async ({ userId, categoryId }: DeleteCategoryPayload) => {
return Categories.destroy({
where: { userId, id: categoryId },
transaction,
});
};

Expand Down
28 changes: 12 additions & 16 deletions src/models/Currencies.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cc from 'currency-codes';
import { Transaction } from 'sequelize/types';
import { Op } from 'sequelize';
import {
Table,
Expand Down Expand Up @@ -85,20 +84,17 @@ export async function getCurrency({
});
}

export async function getCurrencies(
{
ids,
currencies,
numbers,
codes,
}: {
ids?: number[];
numbers?: number[];
currencies?: string[];
codes?: string[];
},
{ transaction }: { transaction?: Transaction } = {},
) {
export async function getCurrencies({
ids,
currencies,
numbers,
codes,
}: {
ids?: number[];
numbers?: number[];
currencies?: string[];
codes?: string[];
}) {
if (
ids === undefined &&
currencies === undefined &&
Expand All @@ -118,7 +114,7 @@ export async function getCurrencies(
if (codes) where.code = { [Op.in]: codes };
if (numbers) where.number = { [Op.in]: numbers };

return Currencies.findAll({ where, transaction });
return Currencies.findAll({ where });
}

export const createCurrency = async ({ code }) => {
Expand Down
35 changes: 17 additions & 18 deletions src/models/RefundTransactions.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Transaction } from 'sequelize/types';
import { Table, Column, Model, ForeignKey, BelongsTo, DataType } from 'sequelize-typescript';
import Transactions from './Transactions.model';
import Users from './Users.model';
Expand Down Expand Up @@ -60,42 +59,42 @@ export default class RefundTransactions extends Model {
refundTransaction: Transactions;
}

export const createRefundTransaction = async (
{
userId,
originalTxId,
refundTxId,
}: { userId: number; originalTxId: number | null; refundTxId: number },
{ transaction }: { transaction?: Transaction } = {},
) => {
return RefundTransactions.create({ userId, originalTxId, refundTxId }, { transaction });
export const createRefundTransaction = async ({
userId,
originalTxId,
refundTxId,
}: {
userId: number;
originalTxId: number | null;
refundTxId: number;
}) => {
return RefundTransactions.create({ userId, originalTxId, refundTxId });
};

export const getRefundsForTransaction = async (
{ originalTxId, userId }: { originalTxId: number; userId: number },
{ transaction }: { transaction?: Transaction } = {},
) => {
export const getRefundsForTransaction = async ({
originalTxId,
userId,
}: {
originalTxId: number;
userId: number;
}) => {
return RefundTransactions.findAll({
where: { originalTxId: originalTxId, userId },
include: [{ model: Transactions, as: 'refundTransaction' }],
transaction,
});
};

export const bulkCreateRefundTransactions = (
{ data }: { data: Array<{ userId: number; originalTxId: number | null; refundTxId: number }> },
{
transaction,
validate = true,
returning = false,
}: {
transaction: Transaction;
validate?: boolean;
returning?: boolean;
},
) => {
return RefundTransactions.bulkCreate(data, {
transaction,
validate,
returning,
});
Expand Down
Loading

0 comments on commit 0471ab9

Please sign in to comment.