Skip to content

Commit

Permalink
implementata modifica costo unitario
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Dec 3, 2024
1 parent 8caf2e1 commit 279dc15
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/admin/cost/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ function CostHistory() {
</tr>
</thead>
<tbody>
{dataHistory.costHistory.map((timestamp: string, cents: number) =>
{dataHistory.costHistory.map(({timestamp,cents}: {
timestamp: string,
cents: number}) =>
<tr key={timestamp}>
<td>{myDate(timestamp)}</td>
<td>{myTime(timestamp)}</td>
Expand Down
18 changes: 11 additions & 7 deletions app/graphql/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ const typeDefs = gql`
const resolvers = {
Query: {
cost: async(_: any, __: {}, context: Context) => {
const db = (await databasePromise).db
const cost = db.collection("cost")
// Find the latest cost based on timestamp
const result = await cost.findOne({}, { sort: { timestamp: -1 } })
return result ? result.cents : 20
return await getCost()
},

costHistory: async(_: any, __: {}, context: Context) => {
Expand Down Expand Up @@ -286,8 +282,8 @@ const resolvers = {
const users = db.collection("users")
const user = await users.findOne({ code })
if (user) {
const COST = await getCost()
const transactions = db.collection("account")
const COST = 20
await transactions.insertOne({
count: 1,
email: user.email,
Expand Down Expand Up @@ -330,9 +326,10 @@ const resolvers = {
const db = (await databasePromise).db
console.log("mutation context:", context)
const account = db.collection("account")
const COST = await getCost()
const result = await account.insertOne({
count: count,
amountCents: -count * 20,
amountCents: -count * COST,
description: "coffee",
email: context.user.email,
timestamp: new Date()
Expand Down Expand Up @@ -407,4 +404,11 @@ const handler = startServerAndCreateNextHandler<NextRequest,Context>(server, {
}
});

async function getCost(): Promise<number> {
const db = (await databasePromise).db
const cost = db.collection("cost")
const result = await cost.findOne({}, { sort: { timestamp: -1 } })
return result ? result.cents : 20
}

export { handler as GET, handler as POST };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dm-coffee",
"version": "1.0.3",
"version": "1.1.0",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down

0 comments on commit 279dc15

Please sign in to comment.