import {Injectable} from "@nestjs/common"; import {PrismaService} from "@/prisma/prisma.service"; import {checkUserHasAccount, checkUserIsAdmin} from "src/utils/checkUser"; @Injectable() export class UserService { constructor(private prisma: PrismaService) {} /** * Retrieves the assets of a given user, including their first name, last name, available dollars, * pseudo, and associated cryptocurrencies. * * @param {string} userId - The unique identifier of the user whose assets are being retrieved. * @return A promise that resolves to an object containing the user's assets and associated data. */ async getMyAssets(userId: string) { await checkUserHasAccount(userId); return this.prisma.user.findUnique({ where: { id: userId, }, select: { firstName: true, lastName: true, dollarAvailables: true, pseudo: true, UserHasCrypto: { select: { Crypto: true, }, }, }, }); } /** * Retrieves the assets of users based on user ID. * * @param {string} userId - The ID of the user requesting the assets. * @return A promise that resolves to an array of user assets. */ async getUsersAssets(userId: string) { await checkUserIsAdmin(userId); return this.prisma.user.findMany({ select: { firstName: true, lastName: true, pseudo: true, dollarAvailables: true, UserHasCrypto: { select: { Crypto: true, amount: true, }, }, }, take: 20, }); } /** * Fetches all trades associated with a given user. * * @param {string} userId - The unique identifier of the user. * @return A promise that resolves to an array of trade objects. */ async getMyTrades(userId: string) { await checkUserHasAccount(userId); return this.prisma.trade.findMany({ where: { OR: [{id_giver: userId}, {id_receiver: userId}], }, include: { Crypto: true, }, }); } }