2022-03-09 17:31:08 +01:00
|
|
|
import { getApp, findTable, makeCall } from "../../utilities"
|
2022-03-08 21:17:07 +01:00
|
|
|
|
2022-03-08 18:29:49 +01:00
|
|
|
async function getSales(req: any) {
|
2022-03-08 21:17:07 +01:00
|
|
|
const { page } = req.query
|
2022-03-08 18:29:49 +01:00
|
|
|
const { _id: appId } = await getApp()
|
2022-03-08 21:17:07 +01:00
|
|
|
const table = await findTable(appId, "sales")
|
|
|
|
return await makeCall("post", `tables/${table._id}/rows/search`, {
|
|
|
|
appId,
|
|
|
|
body: {
|
|
|
|
limit: 10,
|
|
|
|
sort: {
|
|
|
|
type: "string",
|
2022-03-09 16:00:36 +01:00
|
|
|
order: "descending",
|
2022-03-08 21:17:07 +01:00
|
|
|
column: "sale_id",
|
|
|
|
},
|
|
|
|
paginate: true,
|
|
|
|
bookmark: parseInt(page),
|
2022-03-10 11:12:21 +01:00
|
|
|
},
|
2022-03-08 21:17:07 +01:00
|
|
|
})
|
2022-03-08 18:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function saveSale(req: any) {
|
|
|
|
const { _id: appId } = await getApp()
|
2022-03-08 21:17:07 +01:00
|
|
|
const table = await findTable(appId, "sales")
|
|
|
|
return await makeCall("post", `tables/${table._id}/rows`, {
|
|
|
|
body: req.body,
|
|
|
|
appId,
|
|
|
|
})
|
2022-03-08 18:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async function handler(req: any, res: any) {
|
|
|
|
let response: any = {}
|
2022-03-08 21:17:07 +01:00
|
|
|
try {
|
|
|
|
if (req.method === "POST") {
|
|
|
|
response = await saveSale(req)
|
|
|
|
} else if (req.method === "GET") {
|
|
|
|
response = await getSales(req)
|
|
|
|
} else {
|
|
|
|
res.status(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.status(200).json(response)
|
|
|
|
} catch (err: any) {
|
|
|
|
res.status(400).send(err)
|
2022-03-08 18:29:49 +01:00
|
|
|
}
|
2022-03-10 11:12:21 +01:00
|
|
|
}
|