mirror of
https://github.com/System-End/site.git
synced 2026-04-19 20:55:09 +00:00
48 lines
No EOL
1.3 KiB
JavaScript
48 lines
No EOL
1.3 KiB
JavaScript
import AirtablePlus from "airtable-plus"
|
|
|
|
export const shopParts = async () => {
|
|
const baseID = "app4kCWulfB02bV8Q"
|
|
const shopItemsTable = new AirtablePlus({
|
|
apiKey: process.env.AIRTABLE_API_KEY,
|
|
baseID,
|
|
tableName: "Shop Items"
|
|
})
|
|
const ordersTable = new AirtablePlus({
|
|
apiKey: process.env.AIRTABLE_API_KEY,
|
|
baseID,
|
|
tableName: "Orders"
|
|
})
|
|
|
|
const records = await shopItemsTable.read()
|
|
const newRecordsPromise = records.map(async record => {
|
|
const fields = record.fields;
|
|
let stock = fields["Stock"]
|
|
|
|
if (stock && fields["Count of Orders Fulfilled"]) {
|
|
stock -= fields["Count of Orders Fulfilled"]
|
|
}
|
|
return { id: record.id, ...record.fields, "Stock": (stock === null)? null : (stock >= 0 ? stock : 0) }
|
|
})
|
|
|
|
|
|
const newRecords = await Promise.all(newRecordsPromise)
|
|
|
|
return newRecords
|
|
}
|
|
|
|
export default async function handler(req, res) {
|
|
const data = await shopParts()
|
|
|
|
const filteredData = data.filter(record => record["Enabled"]).map(record => {
|
|
return {
|
|
name: record['Name'],
|
|
smallName: record['Small Name'],
|
|
description: record['Description'],
|
|
hours: record['Cost Hours'],
|
|
imageURL: record['Image URL'],
|
|
stock: record['Stock'],
|
|
}
|
|
})
|
|
|
|
return res.json(filteredData)
|
|
} |