Skip to content

Commit

Permalink
[#42] Fix bugs on controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
modejota committed Jan 22, 2023
1 parent 32f6a06 commit aa5d41b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/controllers/bill_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export default async function billController (fastify: FastifyInstance) {
break
}
}
if (Object.keys(product_detailed).length === 0)
return reply.code(404).send({message: 'Product not found in the bill'})
return reply.code(200).send(product_detailed)
}
})
Expand Down Expand Up @@ -131,7 +133,7 @@ export default async function billController (fastify: FastifyInstance) {
handler: async function (request, reply) {
let data = JSON.parse(JSON.stringify(request.body))

const searchClientExists = await Client.findOne({DNI: data.id_cliente})
const searchClientExists = await Client.findOne({DNI: data.idc})
if (!searchClientExists)
return reply.code(404).send({message: 'Client not found. Invoice not created'})

Expand All @@ -141,7 +143,7 @@ export default async function billController (fastify: FastifyInstance) {

let bill = new Bill({
numFactura: data.id,
clienteDNI: data.id_cliente,
clienteDNI: data.idc,
})
await bill.save()
return reply.header('Location', `/invoices/${data.id}`).code(201).send({message: 'Bill inserted into the system successfully'})
Expand Down Expand Up @@ -241,7 +243,7 @@ export default async function billController (fastify: FastifyInstance) {
const searchBillExists = await Bill.findOne({numFactura: data.id})
if (!searchBillExists)
return reply.code(404).send({message: 'Bill not found, so it cannot be deleted'})
await Bill.delete({numFactura: data.id})
await Bill.deleteOne({numFactura: data.id})
return reply.code(200).send({message: 'Bill deleted successfully'})
}
})
Expand Down
29 changes: 8 additions & 21 deletions src/controllers/client_controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FastifyInstance } from "fastify";
import { APIValidators as APIV } from '../constants/api_validators';
import { Constants as C } from "../constants/constants";
import Bill from "../models/bill";
import Client from "../models/client";

export default async function clientController(fastify: FastifyInstance) {
Expand Down Expand Up @@ -107,34 +108,20 @@ export default async function clientController(fastify: FastifyInstance) {
}
})




/*
fastify.route({
method: "GET",
url: "/:id/invoices",
schema: {
params: APIV.ClientID
},
handler: async function (request, reply) {
let ID = JSON.parse(JSON.stringify(request.params)).id
try {
let client = handler.obtener_cliente(ID)
let facturas = handler.get_all_facturas()
let bills = []
for (let factura of facturas) {
if (factura[1].client.id == ID) {
bills.push(factura)
}
}
reply.code(200).send({facturas: bills})
} catch {
logger.error(`Client with ID ${ID} not found so no invoices can be retreived.`)
reply.code(404).send({error: `Client with ID ${ID} not found.`})
}
let data = JSON.parse(JSON.stringify(request.params))
let client = await Client.findOne({DNI: data.id})
if (!client)
return reply.code(404).send({error: `Client with DNI ${data.id} not found.`})
let facturas = await Bill.find({clienteDNI: client.DNI})
return reply.code(200).send(facturas)
}
})
*/

}
}

0 comments on commit aa5d41b

Please sign in to comment.