feat(server): add income target
This commit is contained in:
parent
eda67581ab
commit
f8d766beb0
@ -85,6 +85,8 @@ model IncomeItem {
|
||||
amount Float
|
||||
currency String @default("RUB")
|
||||
count Float @default(1)
|
||||
targetType IncomeItemToType? @map("target_type")
|
||||
targetSourceId String? @map("target_source_id")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
@ -132,3 +134,10 @@ enum ExpenseItemFromType {
|
||||
|
||||
@@map("expense_item_from_types")
|
||||
}
|
||||
|
||||
enum IncomeItemToType {
|
||||
BANK_CARD
|
||||
CASH
|
||||
|
||||
@@map("income_item_to_types")
|
||||
}
|
||||
|
||||
@ -88,6 +88,11 @@ export class BankCardService {
|
||||
await this.bankAccountService.changeBalance(card.bankAccount.id, card.bankAccount.balance - amount);
|
||||
}
|
||||
|
||||
public async addIncome(userId: User['id'], bankCardId: BankCard['id'], amount: number) {
|
||||
const card = await this.findCardById(userId, bankCardId);
|
||||
await this.bankAccountService.changeBalance(card.bankAccount.id, card.bankAccount.balance + amount);
|
||||
}
|
||||
|
||||
private async findCardById(userId: User['id'], bankCardId: BankCard['id']) {
|
||||
const card = await this.prismaService.bankCard.findUnique({
|
||||
where: { id: bankCardId },
|
||||
|
||||
@ -71,4 +71,9 @@ export class CashService {
|
||||
const cash = await this.getCashById(userId, cashId);
|
||||
await this.editCash(userId, cash.id, { balance: cash.balance - amount });
|
||||
}
|
||||
|
||||
public async addIncome(userId: User['id'], cashId: BankCard['id'], amount: number) {
|
||||
const cash = await this.getCashById(userId, cashId);
|
||||
await this.editCash(userId, cash.id, { balance: cash.balance + amount });
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +1,38 @@
|
||||
// eslint-disable-next-line max-classes-per-file
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IncomeItemToType } from '@prisma/client';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsEnum,
|
||||
IsNotEmpty,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsString, ValidateNested,
|
||||
} from 'class-validator';
|
||||
|
||||
import { IsISODateTimeString } from '../../../../shared/validators';
|
||||
|
||||
class IncomeSourceDto {
|
||||
@ApiProperty({
|
||||
description: 'Источник дохода',
|
||||
example: 'CASH',
|
||||
enum: IncomeItemToType,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty({ message: 'Источник дохода обязателен к заполнению' })
|
||||
@IsEnum(IncomeItemToType)
|
||||
type: IncomeItemToType;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'ID карты или наличных ',
|
||||
example: '123',
|
||||
type: String,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty({ message: 'ID источника дохода обязателен к заполнению' })
|
||||
id: string;
|
||||
}
|
||||
|
||||
export class CreateIncomeListItemDto {
|
||||
@ApiProperty({
|
||||
description: 'Заголовок дохода',
|
||||
@ -56,4 +81,14 @@ export class CreateIncomeListItemDto {
|
||||
@IsOptional()
|
||||
@IsString({ message: 'Валюта дохода должна быть строкой.' })
|
||||
currency?: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Источник',
|
||||
type: IncomeSourceDto,
|
||||
nullable: true,
|
||||
})
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => IncomeSourceDto)
|
||||
target?: IncomeSourceDto;
|
||||
}
|
||||
|
||||
@ -1,5 +1,37 @@
|
||||
// eslint-disable-next-line max-classes-per-file
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IncomeItem, IncomeList } from '@prisma/client';
|
||||
import {
|
||||
IncomeItem,
|
||||
IncomeItemToType,
|
||||
IncomeList,
|
||||
} from '@prisma/client';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsEnum,
|
||||
IsNotEmpty,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
|
||||
class IncomeSourceDto {
|
||||
@ApiProperty({
|
||||
description: 'Источник дохода',
|
||||
example: 'CASH',
|
||||
enum: IncomeItemToType,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty({ message: 'Источник дохода обязателен к заполнению' })
|
||||
@IsEnum(IncomeItemToType)
|
||||
type: IncomeItemToType;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'ID карты или наличных ',
|
||||
example: '123',
|
||||
type: String,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty({ message: 'ID источника дохода обязателен к заполнению' })
|
||||
id: string;
|
||||
}
|
||||
|
||||
export class IncomeListItemDto {
|
||||
@ApiProperty({
|
||||
@ -52,6 +84,14 @@ export class IncomeListItemDto {
|
||||
})
|
||||
currency: IncomeItem['currency'];
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Источник',
|
||||
type: IncomeSourceDto,
|
||||
nullable: true,
|
||||
})
|
||||
@Type(() => IncomeSourceDto)
|
||||
target?: IncomeSourceDto | null;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Дата создания',
|
||||
type: Date,
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IncomeItem } from '@prisma/client';
|
||||
|
||||
import { IncomeListItemDto } from '../income-list-item/income-list-item.dto';
|
||||
|
||||
@ -21,7 +20,7 @@ export class IncomeListDto {
|
||||
type: [IncomeListItemDto],
|
||||
nullable: true,
|
||||
})
|
||||
items?: IncomeItem[];
|
||||
items?: IncomeListItemDto[];
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Дата создания',
|
||||
|
||||
@ -1,9 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { BankCardModule } from '@/modules/bank-card/bank-card.module';
|
||||
import { CashModule } from '@/modules/cash/cash.module';
|
||||
|
||||
import { IncomesController } from './incomes.controller';
|
||||
import { IncomesService } from './incomes.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
BankCardModule,
|
||||
CashModule,
|
||||
],
|
||||
controllers: [IncomesController],
|
||||
providers: [IncomesService],
|
||||
})
|
||||
|
||||
@ -1,19 +1,28 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { IncomeItem, IncomeList, User } from '@prisma/client';
|
||||
import {
|
||||
IncomeItem, IncomeItemToType, IncomeList, User,
|
||||
} from '@prisma/client';
|
||||
|
||||
import { BankCardService } from '@/modules/bank-card/bank-card.service';
|
||||
import { CashService } from '@/modules/cash/cash.service';
|
||||
import { PrismaService } from '@/modules/prisma/prisma.service';
|
||||
|
||||
import {
|
||||
CreateIncomeListDto, CreateIncomeListItemDto,
|
||||
CreateIncomeListDto,
|
||||
CreateIncomeListItemDto,
|
||||
DeletedIncomeListDto,
|
||||
EditIncomeListDto, EditIncomeListItemDto,
|
||||
IncomeListDto, IncomeListItemDto,
|
||||
EditIncomeListDto,
|
||||
EditIncomeListItemDto,
|
||||
IncomeListDto,
|
||||
IncomeListItemDto,
|
||||
} from './dto';
|
||||
|
||||
@Injectable()
|
||||
export class IncomesService {
|
||||
constructor(
|
||||
private readonly prismaService: PrismaService,
|
||||
private readonly bankCardService: BankCardService,
|
||||
private readonly cashService: CashService,
|
||||
) {}
|
||||
|
||||
async createList(dto: CreateIncomeListDto, userId: User['id']): Promise<IncomeListDto> {
|
||||
@ -84,9 +93,25 @@ export class IncomesService {
|
||||
date,
|
||||
amount,
|
||||
currency,
|
||||
target,
|
||||
} = dto;
|
||||
const list = await this.findList(listId, userId);
|
||||
|
||||
switch (target?.type) {
|
||||
case IncomeItemToType.BANK_CARD:
|
||||
await this.bankCardService.addIncome(userId, target.id, amount);
|
||||
break;
|
||||
|
||||
case IncomeItemToType.CASH:
|
||||
await this.cashService.addIncome(userId, target.id, amount);
|
||||
break;
|
||||
|
||||
// eslint-disable-next-line no-undefined
|
||||
case undefined:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return this.prismaService.incomeItem.create({
|
||||
data: {
|
||||
title,
|
||||
@ -94,6 +119,8 @@ export class IncomesService {
|
||||
date,
|
||||
amount,
|
||||
currency,
|
||||
targetType: target?.type,
|
||||
targetSourceId: target?.id,
|
||||
incomeList: {
|
||||
connect: { id: list.id },
|
||||
},
|
||||
@ -156,7 +183,29 @@ export class IncomesService {
|
||||
throw new NotFoundException('Лист доходов не найден');
|
||||
}
|
||||
|
||||
return existingList;
|
||||
const { items, ...rest } = existingList;
|
||||
|
||||
return {
|
||||
...rest,
|
||||
items: items.map((item) => {
|
||||
const { targetSourceId, targetType, ...restItem } = item;
|
||||
|
||||
if (targetSourceId && targetType) {
|
||||
return {
|
||||
...restItem,
|
||||
target: {
|
||||
type: targetType,
|
||||
id: targetSourceId,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...restItem,
|
||||
target: null,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
private async findListItem(id: IncomeItem['id'], listId: IncomeList['id'], userId: User['id']) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user