mirror of
https://github.com/linuxserver/budge.git
synced 2026-03-09 00:08:38 +08:00
started converting typeorm to prisma
This commit is contained in:
parent
05d1c75089
commit
ed4fda5a6b
@ -8,6 +8,7 @@ import { Payee } from '../entities/Payee'
|
||||
import { Transaction, TransactionStatus } from '../entities/Transaction'
|
||||
import { Category } from '../entities/Category'
|
||||
import { getRepository } from 'typeorm'
|
||||
import { prisma } from '../prisma'
|
||||
|
||||
@Tags('Accounts')
|
||||
@Route('budgets/{budgetId}/accounts')
|
||||
@ -39,7 +40,7 @@ export class AccountsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<AccountResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne({ id: budgetId, userId: request.user.id })
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId, userId: request.user.id } })
|
||||
if (!budget) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -47,11 +48,12 @@ export class AccountsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const account: Account = getRepository(Account).create({
|
||||
...requestBody,
|
||||
budgetId,
|
||||
const account: Account = await prisma.account.create({
|
||||
data: {
|
||||
...requestBody,
|
||||
budgetId,
|
||||
},
|
||||
})
|
||||
await getRepository(Account).insert(account)
|
||||
|
||||
// Create a transaction for the starting balance of the account
|
||||
if (requestBody.balance !== 0) {
|
||||
@ -63,27 +65,33 @@ export class AccountsController extends Controller {
|
||||
amount = amount * -1 // Inverse balance for CCs
|
||||
break
|
||||
case AccountTypes.Bank:
|
||||
const inflowCategory = await getRepository(Category).findOne({ budgetId: account.budgetId, inflow: true })
|
||||
const inflowCategory = await prisma.category.findUnique({
|
||||
where: { budgetId: account.budgetId, inflow: true },
|
||||
})
|
||||
categoryId = inflowCategory.id
|
||||
break
|
||||
}
|
||||
|
||||
const startingBalancePayee = await getRepository(Payee).findOne({
|
||||
budgetId,
|
||||
name: 'Starting Balance',
|
||||
internal: true,
|
||||
const startingBalancePayee = await prisma.payee.findUnique({
|
||||
where: {
|
||||
budgetId,
|
||||
name: 'Starting Balance',
|
||||
internal: true,
|
||||
},
|
||||
})
|
||||
const startingBalanceTransaction = getRepository(Transaction).create({
|
||||
budgetId,
|
||||
accountId: account.id,
|
||||
payeeId: startingBalancePayee.id,
|
||||
categoryId: categoryId,
|
||||
amount,
|
||||
date: requestBody.date,
|
||||
memo: 'Starting Balance',
|
||||
status: TransactionStatus.Reconciled,
|
||||
|
||||
const startingBalanceTransaction = await prisma.transaction.create({
|
||||
data: {
|
||||
budgetId,
|
||||
accountId: account.id,
|
||||
payeeId: startingBalancePayee.id,
|
||||
categoryId: categoryId,
|
||||
amount,
|
||||
date: requestBody.date,
|
||||
memo: 'Starting Balance',
|
||||
status: TransactionStatus.Reconciled,
|
||||
},
|
||||
})
|
||||
await getRepository(Transaction).insert(startingBalanceTransaction)
|
||||
}
|
||||
|
||||
// Reload account to get the new balanace after the 'initial' transaction was created
|
||||
@ -91,7 +99,7 @@ export class AccountsController extends Controller {
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
data: await (await getRepository(Account).findOne(account.id)).toResponseModel(),
|
||||
data: await (await prisma.account.findUnique({ where: { id: account.id } })).toResponseModel(),
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
@ -128,7 +136,7 @@ export class AccountsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<AccountResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -136,7 +144,7 @@ export class AccountsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
let account = await getRepository(Account).findOne(id)
|
||||
let account = await prisma.account.findUnique({ where: { id } })
|
||||
if (!account) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -151,7 +159,7 @@ export class AccountsController extends Controller {
|
||||
account.order = requestBody.order
|
||||
|
||||
// Update all accounts because of order change
|
||||
let accounts = (await getRepository(Account).find({ budgetId })).map(act => {
|
||||
let accounts: Account[] = (await prisma.account.find({ where: { budgetId } })).map((act: Account) => {
|
||||
if (account.id === act.id) {
|
||||
return account
|
||||
}
|
||||
@ -160,9 +168,11 @@ export class AccountsController extends Controller {
|
||||
})
|
||||
|
||||
accounts = Account.sort(accounts)
|
||||
await getRepository(Account).save(accounts)
|
||||
for (const account of accounts) {
|
||||
await prisma.account.update({ where: { id: account.id }, data: account })
|
||||
}
|
||||
} else {
|
||||
await getRepository(Account).update(account.id, account.getUpdatePayload())
|
||||
await prisma.account.update({ where: { id: account.id }, data: account.getUpdatePayload() })
|
||||
}
|
||||
}
|
||||
|
||||
@ -170,35 +180,42 @@ export class AccountsController extends Controller {
|
||||
// Reconcile the account
|
||||
const difference = requestBody.balance - account.cleared
|
||||
if (difference !== 0) {
|
||||
const reconciliationPayee = await getRepository(Payee).findOne({
|
||||
budgetId,
|
||||
name: 'Reconciliation Balance Adjustment',
|
||||
internal: true,
|
||||
const reconciliationPayee = await prisma.payee.findUnique({
|
||||
where: {
|
||||
budgetId,
|
||||
name: 'Reconciliation Balance Adjustment',
|
||||
internal: true,
|
||||
},
|
||||
})
|
||||
const inflowCategory = await getRepository(Category).findOne({ budgetId: account.budgetId, inflow: true })
|
||||
const startingBalanceTransaction = getRepository(Transaction).create({
|
||||
budgetId,
|
||||
accountId: account.id,
|
||||
payeeId: reconciliationPayee.id,
|
||||
categoryId: inflowCategory.id,
|
||||
amount: difference,
|
||||
date: new Date(),
|
||||
memo: 'Reconciliation Transaction',
|
||||
status: TransactionStatus.Reconciled,
|
||||
const inflowCategory = await prisma.category.findUnique({
|
||||
where: { budgetId: account.budgetId, inflow: true },
|
||||
})
|
||||
const startingBalanceTransaction = await prisma.transaction.create({
|
||||
data: {
|
||||
budgetId,
|
||||
accountId: account.id,
|
||||
payeeId: reconciliationPayee.id,
|
||||
categoryId: inflowCategory.id,
|
||||
amount: difference,
|
||||
date: new Date(),
|
||||
memo: 'Reconciliation Transaction',
|
||||
status: TransactionStatus.Reconciled,
|
||||
},
|
||||
})
|
||||
await getRepository(Transaction).insert(startingBalanceTransaction)
|
||||
}
|
||||
|
||||
const clearedTransactions = await getRepository(Transaction).find({
|
||||
accountId: account.id,
|
||||
status: TransactionStatus.Cleared,
|
||||
const clearedTransactions = await prisma.transaction.find({
|
||||
where: {
|
||||
accountId: account.id,
|
||||
status: TransactionStatus.Cleared,
|
||||
},
|
||||
})
|
||||
for (const transaction of clearedTransactions) {
|
||||
transaction.status = TransactionStatus.Reconciled
|
||||
await getRepository(Transaction).update(transaction.id, transaction.getUpdatePayload())
|
||||
await prisma.transaction.update({ where: { id: transaction.id }, data: transaction.getUpdatePayload() })
|
||||
}
|
||||
|
||||
account = await getRepository(Account).findOne(account.id)
|
||||
account = await prisma.account.findUnique({ where: { id: account.id } })
|
||||
}
|
||||
|
||||
return {
|
||||
@ -239,7 +256,7 @@ export class AccountsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<AccountsResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne({ id: budgetId, userId: request.user.id })
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId, userId: request.user.id } })
|
||||
if (!budget) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -247,7 +264,7 @@ export class AccountsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const accounts = await getRepository(Account).find({ where: { budgetId } })
|
||||
const accounts: Account[] = await prisma.account.find({ where: { budgetId } })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -285,7 +302,7 @@ export class AccountsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<AccountResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne({ id: budgetId, userId: request.user.id })
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId, userId: request.user.id } })
|
||||
if (!budget) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -293,7 +310,7 @@ export class AccountsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const account = await getRepository(Account).findOne(accountId)
|
||||
const account = await prisma.account.findUnique({ where: { id: accountId } })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
|
||||
@ -9,6 +9,8 @@ import { BudgetMonthsResponse, BudgetMonthWithCategoriesResponse } from '../mode
|
||||
import { getCustomRepository, getRepository, MoreThanOrEqual } from 'typeorm'
|
||||
import { BudgetMonths } from '../repositories/BudgetMonths'
|
||||
import { getMonthStringFromNow } from '../utils'
|
||||
import { prisma } from '../prisma'
|
||||
import { CategoryMonth } from '../entities/CategoryMonth'
|
||||
|
||||
@Tags('Budgets')
|
||||
@Route('budgets')
|
||||
@ -55,7 +57,10 @@ export class BudgetsController extends Controller {
|
||||
})
|
||||
public async getBudgets(@Request() request: ExpressRequest): Promise<BudgetsResponse | ErrorResponse> {
|
||||
try {
|
||||
const budgets = await getRepository(Budget).find({ where: { userId: request.user.id }, relations: ['accounts'] })
|
||||
const budgets: Budget[] = await prisma.budget.find({
|
||||
where: { userId: request.user.id },
|
||||
include: { accounts: true },
|
||||
})
|
||||
return {
|
||||
message: 'success',
|
||||
data: await Promise.all(budgets.map(budget => budget.toResponseModel())),
|
||||
@ -87,9 +92,12 @@ export class BudgetsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<BudgetResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget: Budget = getRepository(Budget).create({ ...requestBody })
|
||||
budget.user = request.user
|
||||
await getRepository(Budget).insert(budget)
|
||||
const budget: Budget = await prisma.budget.create({
|
||||
data: {
|
||||
...requestBody,
|
||||
userId: request.user.id,
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -122,7 +130,7 @@ export class BudgetsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<BudgetResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget: Budget = await getRepository(Budget).findOne(id)
|
||||
const budget: Budget = await prisma.budget.findUnique({ where: { id } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -165,7 +173,7 @@ export class BudgetsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<BudgetResponse | ErrorResponse> {
|
||||
try {
|
||||
let budget: Budget = await getRepository(Budget).findOne(id)
|
||||
let budget: Budget = await prisma.budget.findUnique({ where: { id } })
|
||||
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
@ -174,8 +182,8 @@ export class BudgetsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
budget = budget.update(requestBody)
|
||||
await getRepository(Budget).update(budget.id, budget.getUpdatePayload())
|
||||
Object.assign(budget, requestBody)
|
||||
await prisma.budget.update({ where: { id: budget.id }, data: budget })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -212,7 +220,7 @@ export class BudgetsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
@Query() from?: string,
|
||||
): Promise<BudgetMonthsResponse | ErrorResponse> {
|
||||
let budget: Budget = await getRepository(Budget).findOne(budgetId)
|
||||
let budget: Budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
@ -221,10 +229,10 @@ export class BudgetsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const budgetMonths = await getRepository(BudgetMonth).find({
|
||||
const budgetMonths: BudgetMonth[] = await prisma.budgetMonth.find({
|
||||
where: {
|
||||
budgetId,
|
||||
...(from && { month: MoreThanOrEqual(from) }),
|
||||
...(from && { month: { gte: from } }),
|
||||
},
|
||||
})
|
||||
|
||||
@ -270,7 +278,7 @@ export class BudgetsController extends Controller {
|
||||
@Path() month: string,
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<BudgetMonthWithCategoriesResponse | ErrorResponse> {
|
||||
let budget: Budget = await getRepository(Budget).findOne(budgetId)
|
||||
let budget: Budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
@ -279,18 +287,19 @@ export class BudgetsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
let budgetMonth = await getRepository(BudgetMonth).findOne({
|
||||
let budgetMonth = await prisma.budgetMonth.findUnique({
|
||||
where: { budgetId, month },
|
||||
relations: ['categories'],
|
||||
include: { categories: true },
|
||||
})
|
||||
if (!budgetMonth) {
|
||||
// If we don't have a budget month, then no transactions were created against that month,
|
||||
// so send down an 'empty' budget month for the UI to work with
|
||||
budgetMonth = getRepository(BudgetMonth).create({
|
||||
budgetId,
|
||||
month,
|
||||
budgetMonth = await prisma.budgetMonth.create({
|
||||
data: {
|
||||
budgetId,
|
||||
month,
|
||||
},
|
||||
})
|
||||
budgetMonth.categories = Promise.resolve([])
|
||||
}
|
||||
|
||||
return {
|
||||
@ -298,7 +307,7 @@ export class BudgetsController extends Controller {
|
||||
data: {
|
||||
...(await budgetMonth.toResponseModel()),
|
||||
categories: await Promise.all(
|
||||
(await budgetMonth.categories).map(categoryMonth => categoryMonth.toResponseModel()),
|
||||
(await budgetMonth.categories).map((categoryMonth: CategoryMonth) => categoryMonth.toResponseModel()),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
||||
@ -11,6 +11,7 @@ import { CategoryMonthRequest, CategoryMonthResponse, CategoryMonthsResponse } f
|
||||
import { CategoryMonth } from '../entities/CategoryMonth'
|
||||
import { getCustomRepository, getRepository, MoreThanOrEqual } from 'typeorm'
|
||||
import { CategoryMonths } from '../repositories/CategoryMonths'
|
||||
import { prisma } from '../prisma'
|
||||
|
||||
@Tags('Categories')
|
||||
@Route('budgets/{budgetId}/categories')
|
||||
@ -41,7 +42,7 @@ export class CategoriesController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<CategoryGroupsResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -49,7 +50,7 @@ export class CategoriesController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const categoryGroups: CategoryGroup[] = await getRepository(CategoryGroup).find({ where: { budgetId } })
|
||||
const categoryGroups: CategoryGroup[] = await prisma.categoryGroup.find({ where: { budgetId } })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -85,7 +86,7 @@ export class CategoriesController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<CategoryGroupResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -93,11 +94,12 @@ export class CategoriesController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const categoryGroup: CategoryGroup = getRepository(CategoryGroup).create({
|
||||
...requestBody,
|
||||
budgetId,
|
||||
const categoryGroup: CategoryGroup = await prisma.categoryGroup.create({
|
||||
data: {
|
||||
...requestBody,
|
||||
budgetId,
|
||||
},
|
||||
})
|
||||
await getRepository(CategoryGroup).insert(categoryGroup)
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -134,7 +136,7 @@ export class CategoriesController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<CategoryGroupResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -142,14 +144,14 @@ export class CategoriesController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const categoryGroup = await getRepository(CategoryGroup).findOne(id)
|
||||
const categoryGroup = await prisma.categoryGroup.findUnique({ where: { id } })
|
||||
categoryGroup.name = requestBody.name
|
||||
|
||||
if (categoryGroup.order !== requestBody.order) {
|
||||
// re-order category groups
|
||||
categoryGroup.order = requestBody.order
|
||||
|
||||
let categoryGroups = (await getRepository(CategoryGroup).find({ budgetId })).map(group => {
|
||||
let categoryGroups = (await prisma.categoryGroup.find({ where: { budgetId } })).map((group: CategoryGroup) => {
|
||||
if (group.id === categoryGroup.id) {
|
||||
return categoryGroup
|
||||
}
|
||||
@ -159,9 +161,11 @@ export class CategoriesController extends Controller {
|
||||
|
||||
categoryGroups = CategoryGroup.sort(categoryGroups)
|
||||
|
||||
await getRepository(CategoryGroup).save(categoryGroups)
|
||||
for (const categoryGroup of categoryGroups) {
|
||||
await prisma.categoryGroup.update({ where: { id: categoryGroup.id }, data: categoryGroup })
|
||||
}
|
||||
} else {
|
||||
await getRepository(CategoryGroup).update(categoryGroup.id, categoryGroup.getUpdatePayload())
|
||||
await prisma.categoryGroup.update({ where: { id: categoryGroup.id }, data: categoryGroup.getUpdatePayload() })
|
||||
}
|
||||
|
||||
return {
|
||||
@ -198,7 +202,7 @@ export class CategoriesController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<CategoryResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -206,11 +210,12 @@ export class CategoriesController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const category: Category = getRepository(Category).create({
|
||||
...requestBody,
|
||||
budgetId,
|
||||
const category: Category = await prisma.category.create({
|
||||
data: {
|
||||
...requestBody,
|
||||
budgetId,
|
||||
},
|
||||
})
|
||||
await getRepository(Category).insert(category)
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -247,7 +252,7 @@ export class CategoriesController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<CategoryResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -255,7 +260,7 @@ export class CategoriesController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const category = await getRepository(Category).findOne(id, { relations: ['categoryGroup'] })
|
||||
const category = await prisma.category.findUnique({ where: { id } }, { includes: { categoryGroups: true } })
|
||||
|
||||
const originalCategoryGroupId = category.categoryGroupId
|
||||
const updateOrder =
|
||||
@ -267,7 +272,9 @@ export class CategoriesController extends Controller {
|
||||
category.categoryGroupId = requestBody.categoryGroupId
|
||||
|
||||
if (updateOrder === true) {
|
||||
let categories = await getRepository(Category).find({ categoryGroupId: category.categoryGroupId })
|
||||
let categories: Category[] = await prisma.category.find({
|
||||
where: { categoryGroupId: category.categoryGroupId },
|
||||
})
|
||||
if (originalCategoryGroupId !== category.categoryGroupId) {
|
||||
categories.push(category)
|
||||
} else {
|
||||
@ -275,9 +282,11 @@ export class CategoriesController extends Controller {
|
||||
}
|
||||
|
||||
categories = Category.sort(categories)
|
||||
await getRepository(Category).save(categories)
|
||||
for (const category of categories) {
|
||||
await prisma.category.update({ where: { id: category.id }, data: category })
|
||||
}
|
||||
} else {
|
||||
await getRepository(Category).update(category.id, category.getUpdatePayload())
|
||||
await prisma.category.update({ where: { id: category.id }, data: category.getUpdatePayload() })
|
||||
}
|
||||
|
||||
return {
|
||||
@ -316,7 +325,7 @@ export class CategoriesController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<CategoryMonthResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -326,7 +335,7 @@ export class CategoriesController extends Controller {
|
||||
|
||||
const categoryMonth = await getCustomRepository(CategoryMonths).findOrCreate(budgetId, categoryId, month)
|
||||
categoryMonth.update({ budgeted: requestBody.budgeted })
|
||||
await getRepository(CategoryMonth).update(categoryMonth.id, categoryMonth.getUpdatePayload())
|
||||
await prisma.categoryMonth.update({ where: { id: categoryMonth.id }, data: categoryMonth.getUpdatePayload() })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -365,7 +374,7 @@ export class CategoriesController extends Controller {
|
||||
@Query() from?: string,
|
||||
): Promise<CategoryMonthsResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -376,10 +385,10 @@ export class CategoriesController extends Controller {
|
||||
const findParams = {
|
||||
where: {
|
||||
categoryId,
|
||||
...(from && { month: MoreThanOrEqual(from) }),
|
||||
...(from && { month: { gte: from } }),
|
||||
},
|
||||
}
|
||||
const categoryMonths = await getRepository(CategoryMonth).find(findParams)
|
||||
const categoryMonths: CategoryMonth[] = await prisma.categoryMonth.find(findParams)
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
|
||||
@ -5,6 +5,7 @@ import { ErrorResponse } from './responses'
|
||||
import { PayeeRequest, PayeeResponse, PayeesResponse } from '../models/Payee'
|
||||
import { Payee } from '../entities/Payee'
|
||||
import { getRepository } from 'typeorm'
|
||||
import { prisma } from '../prisma'
|
||||
|
||||
@Tags('Payees')
|
||||
@Route('budgets/{budgetId}/payees')
|
||||
@ -31,7 +32,7 @@ export class PayeesController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<PayeeResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -39,11 +40,12 @@ export class PayeesController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const payee = getRepository(Payee).create({
|
||||
...requestBody,
|
||||
budgetId,
|
||||
const payee = await prisma.payee.create({
|
||||
data: {
|
||||
...requestBody,
|
||||
budgetId,
|
||||
},
|
||||
})
|
||||
await getRepository(Payee).insert(payee)
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -77,7 +79,7 @@ export class PayeesController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<PayeesResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -85,7 +87,7 @@ export class PayeesController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const payees = await getRepository(Payee).find({ where: { budgetId } })
|
||||
const payees = await prisma.payee.find({ where: { budgetId } })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -118,7 +120,7 @@ export class PayeesController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<PayeeResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -126,7 +128,7 @@ export class PayeesController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const payee = await getRepository(Payee).findOne(payeeId)
|
||||
const payee = await prisma.payee.findUnique({ where: { id: payeeId } })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
|
||||
@ -4,6 +4,7 @@ import { User } from '../entities/User'
|
||||
import { LoginRequest, ExpressRequest } from './requests'
|
||||
import { ErrorResponse } from './responses'
|
||||
import { getRepository } from 'typeorm'
|
||||
import { prisma } from '../prisma'
|
||||
|
||||
@Route()
|
||||
export class RootController extends Controller {
|
||||
@ -27,7 +28,7 @@ export class RootController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<LoginResponse | ErrorResponse> {
|
||||
const { email, password } = requestBody
|
||||
const user: User = await getRepository(User).findOne({ email })
|
||||
const user: User = await prisma.user.findUnique({ where: { email } })
|
||||
|
||||
if (!user) {
|
||||
this.setStatus(403)
|
||||
@ -110,7 +111,7 @@ export class RootController extends Controller {
|
||||
})
|
||||
public async getMe(@Request() request: ExpressRequest): Promise<UserResponse | ErrorResponse> {
|
||||
try {
|
||||
const user: User = await getRepository(User).findOne({ email: request.user.email })
|
||||
const user: User = await prisma.user.findUnique({ where: { email: request.user.email } })
|
||||
|
||||
return {
|
||||
data: await user.toResponseModel(),
|
||||
|
||||
@ -11,7 +11,7 @@ import {
|
||||
TransactionsRequest,
|
||||
TransactionsResponse,
|
||||
} from '../models/Transaction'
|
||||
import { getManager, getRepository, In } from 'typeorm'
|
||||
import { prisma } from '../prisma'
|
||||
|
||||
@Tags('Budgets')
|
||||
@Route('budgets/{budgetId}')
|
||||
@ -42,7 +42,7 @@ export class TransactionsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<TransactionResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -50,16 +50,12 @@ export class TransactionsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const transaction = await getManager().transaction(async transactionalEntityManager => {
|
||||
const transaction = transactionalEntityManager.getRepository(Transaction).create({
|
||||
const transaction = await prisma.transaction.create({
|
||||
data: {
|
||||
budgetId,
|
||||
...requestBody,
|
||||
date: new Date(requestBody.date),
|
||||
})
|
||||
TransactionCache.enableTransfers(transaction.id)
|
||||
await transactionalEntityManager.getRepository(Transaction).insert(transaction)
|
||||
|
||||
return transaction
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
@ -100,7 +96,7 @@ export class TransactionsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<TransactionsResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -108,19 +104,18 @@ export class TransactionsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const transactions = requestBody.transactions.map(transaction => {
|
||||
return getRepository(Transaction).create({
|
||||
budgetId,
|
||||
...transaction,
|
||||
date: new Date(transaction.date),
|
||||
})
|
||||
})
|
||||
|
||||
await getManager().transaction(async transactionalEntityManager => {
|
||||
for (const transaction of transactions) {
|
||||
await transactionalEntityManager.getRepository(Transaction).insert(transaction)
|
||||
}
|
||||
})
|
||||
const transactions: Transaction[] = []
|
||||
for (const transaction of requestBody.transactions) {
|
||||
transactions.push(
|
||||
await prisma.transaction.create({
|
||||
data: {
|
||||
budgetId,
|
||||
...transaction,
|
||||
date: new Date(transaction.date),
|
||||
},
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -159,7 +154,7 @@ export class TransactionsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<TransactionResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -170,20 +165,13 @@ export class TransactionsController extends Controller {
|
||||
// Load in original transaction to check if the amount has been altered
|
||||
// and updated the category month accordingly
|
||||
// @TODO: remove relation to test db transactions
|
||||
const transaction = await getManager().transaction(async transactionalEntityManager => {
|
||||
const transaction = await transactionalEntityManager
|
||||
.getRepository(Transaction)
|
||||
.findOne(transactionId, { relations: ['account'] })
|
||||
transaction.update({
|
||||
TransactionCache.enableTransfers(transactionId)
|
||||
const transaction: Transaction = await prisma.transaction.update({
|
||||
where: { id: transactionId },
|
||||
data: {
|
||||
...requestBody,
|
||||
...(requestBody.date && { date: new Date(requestBody.date) }), // @TODO: this is hacky and I don't like it, but the update keeps date as a string and breaks the sanitize function
|
||||
})
|
||||
TransactionCache.enableTransfers(transaction.id)
|
||||
await transactionalEntityManager
|
||||
.getRepository(Transaction)
|
||||
.update(transaction.id, transaction.getUpdatePayload())
|
||||
|
||||
return transaction
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
@ -224,7 +212,7 @@ export class TransactionsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<TransactionsResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -240,25 +228,20 @@ export class TransactionsController extends Controller {
|
||||
{},
|
||||
)
|
||||
|
||||
const transactions = await getManager().transaction(async transactionalEntityManager => {
|
||||
const transactions = await transactionalEntityManager
|
||||
.getRepository(Transaction)
|
||||
.find({ where: { id: In(Object.keys(transactionsMap)) } })
|
||||
transactions.map(transaction =>
|
||||
transaction.update({
|
||||
const transactions: Transaction[] = await prisma.transaction.find({
|
||||
where: { id: { in: Object.keys(transactionsMap) } },
|
||||
})
|
||||
|
||||
for (const transaction of transactions) {
|
||||
await prisma.transaction.update({
|
||||
where: { id: transaction.id },
|
||||
data: {
|
||||
...transactionsMap[transaction.id],
|
||||
amount: transaction.amount,
|
||||
date: transaction.date,
|
||||
}),
|
||||
)
|
||||
|
||||
for (const transaction of transactions) {
|
||||
await transactionalEntityManager.getRepository(Transaction).save(transaction)
|
||||
}
|
||||
// await transactionalEntityManager.getRepository(Transaction).save(transactions)
|
||||
|
||||
return transactions
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -284,7 +267,7 @@ export class TransactionsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<TransactionResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -292,9 +275,7 @@ export class TransactionsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const transaction = await getRepository(Transaction).findOne(transactionId)
|
||||
TransactionCache.enableTransfers(transactionId)
|
||||
await getRepository(Transaction).remove(transaction)
|
||||
const transaction = await prisma.transaction.delete({ where: { id: transactionId } })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -319,7 +300,7 @@ export class TransactionsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<TransactionsResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -327,12 +308,9 @@ export class TransactionsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const transactions = await getManager()
|
||||
.getRepository(Transaction)
|
||||
.find({ where: { id: In(requestBody.ids) } })
|
||||
requestBody.ids.map(id => TransactionCache.enableTransfers(id))
|
||||
const transactions: Transaction[] = await prisma.transaction.find({ where: { id: { in: [requestBody.ids] } } })
|
||||
for (const transaction of transactions) {
|
||||
await getManager().getRepository(Transaction).remove(transaction)
|
||||
await prisma.transaction.delete({ where: { id: transaction.id } })
|
||||
}
|
||||
|
||||
return {
|
||||
@ -372,7 +350,7 @@ export class TransactionsController extends Controller {
|
||||
@Request() request: ExpressRequest,
|
||||
): Promise<TransactionsResponse | ErrorResponse> {
|
||||
try {
|
||||
const budget = await getRepository(Budget).findOne(budgetId)
|
||||
const budget = await prisma.budget.findUnique({ where: { id: budgetId } })
|
||||
if (!budget || budget.userId !== request.user.id) {
|
||||
this.setStatus(404)
|
||||
return {
|
||||
@ -380,11 +358,13 @@ export class TransactionsController extends Controller {
|
||||
}
|
||||
}
|
||||
|
||||
const account = await getRepository(Account).findOne(accountId, { relations: ['transactions'] })
|
||||
const account = await prisma.account.findUnique({ where: { id: accountId }, include: { transactions: true } })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
data: await Promise.all((await account.transactions).map(transaction => transaction.toResponseModel())),
|
||||
data: await Promise.all(
|
||||
(await account.transactions).map((transaction: Transaction) => transaction.toResponseModel()),
|
||||
),
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
|
||||
@ -4,6 +4,7 @@ import { User } from '../entities/User'
|
||||
import { ExpressRequest, UserCreateRequest, UserUpdateRequest } from './requests'
|
||||
import { ErrorResponse } from './responses'
|
||||
import { getManager, getRepository } from 'typeorm'
|
||||
import { prisma } from '../prisma'
|
||||
|
||||
@Tags('Users')
|
||||
@Route('users')
|
||||
@ -24,18 +25,14 @@ export class UsersController extends Controller {
|
||||
public async createUser(@Body() requestBody: UserCreateRequest): Promise<UserResponse | ErrorResponse> {
|
||||
const { email } = requestBody
|
||||
|
||||
const emailCheck: User = await getRepository(User).findOne({ email })
|
||||
const emailCheck: User = await prisma.user.findUnique({ where: { email } })
|
||||
if (emailCheck) {
|
||||
this.setStatus(400)
|
||||
return { message: 'Email already exists' }
|
||||
}
|
||||
|
||||
try {
|
||||
const newUser = await getManager().transaction(async transactionalEntityManager => {
|
||||
const newUser: User = transactionalEntityManager.getRepository(User).create({ ...requestBody })
|
||||
await transactionalEntityManager.getRepository(User).insert(newUser)
|
||||
return newUser
|
||||
})
|
||||
const newUser = await prisma.user.create({ data: requestBody })
|
||||
|
||||
return {
|
||||
message: 'success',
|
||||
@ -65,7 +62,7 @@ export class UsersController extends Controller {
|
||||
})
|
||||
public async getUserByEmail(@Path() email: string): Promise<UserResponse | ErrorResponse> {
|
||||
try {
|
||||
const user: User = await getRepository(User).findOne({ email })
|
||||
const user: User = await prisma.user.findUnique({ where: { email } })
|
||||
|
||||
return {
|
||||
data: await user.toResponseModel(),
|
||||
@ -119,7 +116,7 @@ export class UsersController extends Controller {
|
||||
}
|
||||
|
||||
try {
|
||||
await getRepository(User).save(request.user)
|
||||
await prisma.user.update({ where: { id: request.user.id }, data: request.user })
|
||||
|
||||
return {
|
||||
data: await request.user.toResponseModel(),
|
||||
|
||||
@ -4,6 +4,7 @@ import { User } from '../entities/User'
|
||||
import config from '../config'
|
||||
import { logger } from '../config/winston'
|
||||
import { getRepository } from 'typeorm'
|
||||
import { prisma } from '../prisma'
|
||||
|
||||
export async function expressAuthentication(
|
||||
request: Request,
|
||||
@ -24,7 +25,7 @@ export async function expressAuthentication(
|
||||
jwtPayload = jwt.verify(token, config.jwtSecret) as any
|
||||
const { userId, email } = jwtPayload
|
||||
|
||||
user = await getRepository(User).findOne(userId)
|
||||
user = await prisma.user.findUnique({ where: { id: userId } })
|
||||
} catch (err) {}
|
||||
}
|
||||
|
||||
|
||||
3
backend/src/prisma.ts
Normal file
3
backend/src/prisma.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
export const prisma = new PrismaClient()
|
||||
Loading…
x
Reference in New Issue
Block a user