From 90b47abc24e630e5ebecb3768ae6eee3217b2338 Mon Sep 17 00:00:00 2001 From: Ricardo <ricardofaria170@gmail.com> Date: Tue, 25 Mar 2025 10:55:09 -0300 Subject: [PATCH] FIX implant must not have owner to be sold --- src/handlers/implants.ts | 55 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/handlers/implants.ts b/src/handlers/implants.ts index 7e81b92..9a12f5b 100644 --- a/src/handlers/implants.ts +++ b/src/handlers/implants.ts @@ -1,8 +1,9 @@ import { type Request, type Response } from 'express'; -import { implantsTable } from '@/db/schema'; +import { implantsTable, purchasesTable, usersTable} from '@/db/schema'; import { db } from "@/db"; import { eq } from 'drizzle-orm'; import { implantSchema, updateImplantSchema } from '@/validators/implantsValidator'; +import { error } from 'console'; export default class implant{ static implantRequestValidation (req: Request){ @@ -101,7 +102,7 @@ export default class implant{ } static async implantCreate(req: Request, res: Response){ - const implant_package = implant.implantRequestUnzip(req);setInterval + const implant_package = implant.implantRequestUnzip(req); if (!implant_package.success) { return res.status(400).json("Invalid Request"); @@ -122,4 +123,54 @@ export default class implant{ return res.status(500).json({ error: "User Creation Error"}); } } + + static async implantBuy (req: Request, res: Response): Promise<any>{ + try{ + const { buyer_id, product_id } = req.body; + + const user = await db.select().from(usersTable).where(eq(usersTable.id, buyer_id)); + const cyberPart = await db.select().from(implantsTable).where(eq(implantsTable.id, product_id)); + + if (!user || !cyberPart) + return res.status(404).json({ error: "User or implant not found" }); + + const bought = await db.select().from(purchasesTable).where(eq(purchasesTable.implant_id, product_id)) + if (bought) + return res.status(404).json({ error: "Implant was already bought" }); + + var userMoney = parseFloat(user.money); + const implantPrice = parseFloat(cyberPart.price); + var userCyberPsychosis = parseInt(user.cyberpsychosis, 10); + const userCyberLimit = parseInt(user.cyberLimit, 10); + const implantCyberCost = parseInt(cyberPart.cyberCost, 10) + + if (userMoney < implantPrice) + return res.status(404).json({ error: "Insufficient money" }); + + userMoney -= implantPrice; + userCyberPsychosis += implantCyberCost; + + if (userCyberPsychosis >= userCyberLimit) + return res.status(404).json({ error: "Impossible to use implant, risk of cyberpsychosis is too high" }); + + const updated_user = await db.update(usersTable) + .set({ + money: userMoney.toString(), + cyberpsychosis: userCyberPsychosis.toString() + }).where(eq(usersTable.id, buyer_id)).returning(); + + const purchase = await db.insert(purchasesTable) + .values({ + user_id: buyer_id, + implant_id: product_id, + }).returning(); + + return res.status(200) + .json({ message: "Implant bought successfully", + purchase, user: updated_user }); + } catch (error) { + console.error(error); + return res.status(500).json({ error: "Erro ao realizar a compra" }); + } + } } -- GitLab