120 lines
4.6 KiB
JavaScript
120 lines
4.6 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.TransactionsService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const prisma_service_1 = require("../prisma/prisma.service");
|
|
const user_util_1 = require("../common/user.util");
|
|
let TransactionsService = class TransactionsService {
|
|
prisma;
|
|
constructor(prisma) {
|
|
this.prisma = prisma;
|
|
}
|
|
userId() {
|
|
return (0, user_util_1.getTempUserId)();
|
|
}
|
|
list(walletId) {
|
|
return this.prisma.transaction.findMany({
|
|
where: { userId: this.userId(), walletId },
|
|
orderBy: { date: 'desc' },
|
|
take: 200,
|
|
});
|
|
}
|
|
listAll() {
|
|
return this.prisma.transaction.findMany({
|
|
where: { userId: this.userId() },
|
|
orderBy: { date: 'desc' },
|
|
take: 1000,
|
|
});
|
|
}
|
|
listWithFilters(walletId, filters) {
|
|
const where = {
|
|
userId: (0, user_util_1.getTempUserId)(),
|
|
walletId,
|
|
};
|
|
if (filters.direction)
|
|
where.direction = filters.direction;
|
|
if (filters.category)
|
|
where.category = filters.category;
|
|
if (filters.from || filters.to) {
|
|
where.date = {};
|
|
if (filters.from)
|
|
where.date.gte = new Date(filters.from);
|
|
if (filters.to)
|
|
where.date.lte = new Date(filters.to);
|
|
}
|
|
return this.prisma.transaction.findMany({
|
|
where,
|
|
orderBy: { date: 'desc' },
|
|
});
|
|
}
|
|
async create(walletId, input) {
|
|
const amountNum = typeof input.amount === 'string' ? Number(input.amount) : input.amount;
|
|
if (!Number.isFinite(amountNum))
|
|
throw new Error('amount must be a number');
|
|
const date = input.date ? new Date(input.date) : new Date();
|
|
const wallet = await this.prisma.wallet.findFirst({
|
|
where: { id: walletId, userId: this.userId(), deletedAt: null },
|
|
select: { id: true },
|
|
});
|
|
if (!wallet)
|
|
throw new Error('wallet not found');
|
|
return this.prisma.transaction.create({
|
|
data: {
|
|
userId: this.userId(),
|
|
walletId,
|
|
amount: amountNum,
|
|
direction: input.direction,
|
|
date,
|
|
category: input.category ?? null,
|
|
memo: input.memo ?? null,
|
|
},
|
|
});
|
|
}
|
|
async update(walletId, id, dto) {
|
|
const existing = await this.prisma.transaction.findFirst({
|
|
where: { id, walletId, userId: this.userId() },
|
|
});
|
|
if (!existing)
|
|
throw new Error('transaction not found');
|
|
const data = {};
|
|
if (dto.amount !== undefined)
|
|
data.amount = Number(dto.amount);
|
|
if (dto.direction)
|
|
data.direction = dto.direction;
|
|
if (dto.category !== undefined)
|
|
data.category = dto.category || null;
|
|
if (dto.memo !== undefined)
|
|
data.memo = dto.memo || null;
|
|
if (dto.date !== undefined)
|
|
data.date = new Date(dto.date);
|
|
return this.prisma.transaction.update({
|
|
where: { id: existing.id },
|
|
data,
|
|
});
|
|
}
|
|
async delete(walletId, id) {
|
|
const existing = await this.prisma.transaction.findFirst({
|
|
where: { id, walletId, userId: this.userId() },
|
|
});
|
|
if (!existing)
|
|
throw new Error('transaction not found');
|
|
return this.prisma.transaction.delete({
|
|
where: { id: existing.id },
|
|
});
|
|
}
|
|
};
|
|
exports.TransactionsService = TransactionsService;
|
|
exports.TransactionsService = TransactionsService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
|
], TransactionsService);
|
|
//# sourceMappingURL=transactions.service.js.map
|