86 lines
3.4 KiB
JavaScript
86 lines
3.4 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.WalletsService = 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 WalletsService = class WalletsService {
|
|
prisma;
|
|
constructor(prisma) {
|
|
this.prisma = prisma;
|
|
}
|
|
userId() {
|
|
return (0, user_util_1.getTempUserId)();
|
|
}
|
|
list() {
|
|
return this.prisma.wallet.findMany({
|
|
where: { userId: this.userId(), deletedAt: null },
|
|
orderBy: { createdAt: 'asc' },
|
|
});
|
|
}
|
|
create(input) {
|
|
const kind = input.kind ?? 'money';
|
|
return this.prisma.wallet.create({
|
|
data: {
|
|
userId: this.userId(),
|
|
name: input.name,
|
|
kind,
|
|
currency: kind === 'money' ? (input.currency ?? 'IDR') : null,
|
|
unit: kind === 'asset' ? (input.unit ?? null) : null,
|
|
initialAmount: input.initialAmount || null,
|
|
pricePerUnit: kind === 'asset' ? (input.pricePerUnit || null) : null,
|
|
},
|
|
});
|
|
}
|
|
update(id, input) {
|
|
const updateData = {};
|
|
if (input.name !== undefined)
|
|
updateData.name = input.name;
|
|
if (input.kind !== undefined) {
|
|
updateData.kind = input.kind;
|
|
if (input.kind === 'money') {
|
|
updateData.currency = input.currency ?? 'IDR';
|
|
updateData.unit = null;
|
|
}
|
|
else {
|
|
updateData.unit = input.unit ?? null;
|
|
updateData.currency = null;
|
|
}
|
|
}
|
|
else {
|
|
if (input.currency !== undefined)
|
|
updateData.currency = input.currency;
|
|
if (input.unit !== undefined)
|
|
updateData.unit = input.unit;
|
|
}
|
|
if (input.initialAmount !== undefined)
|
|
updateData.initialAmount = input.initialAmount || null;
|
|
if (input.pricePerUnit !== undefined)
|
|
updateData.pricePerUnit = input.pricePerUnit || null;
|
|
return this.prisma.wallet.update({
|
|
where: { id, userId: this.userId() },
|
|
data: updateData,
|
|
});
|
|
}
|
|
delete(id) {
|
|
return this.prisma.wallet.update({
|
|
where: { id, userId: this.userId() },
|
|
data: { deletedAt: new Date() },
|
|
});
|
|
}
|
|
};
|
|
exports.WalletsService = WalletsService;
|
|
exports.WalletsService = WalletsService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
|
], WalletsService);
|
|
//# sourceMappingURL=wallets.service.js.map
|