| 3 | const prisma = new PrismaClient(); |
| 4 | |
| 5 | export const getCurrentMonthUsage = async (endUser: string): Promise<number> => { |
| 6 | const now = new Date(); |
| 7 | const start = new Date(now.getFullYear(), now.getMonth(), 1); |
| 8 | const end = new Date(now.getFullYear(), now.getMonth() + 1, 1); |
| 9 | const aggregations = await prisma.usage.aggregate({ |
| 10 | _sum: { |
| 11 | count: true, |
| 12 | }, |
| 13 | where: { |
| 14 | endUser: endUser, |
| 15 | createdAt: { |
| 16 | gte: start, |
| 17 | lt: end, |
| 18 | }, |
| 19 | }, |
| 20 | }); |
| 21 | |
| 22 | return aggregations._sum.count || 0; |
| 23 | }; |
| 24 | |
| 25 | // We coerce individual usage to the begining of the day to reduce the usage records. |
| 26 | export const addUsage = async (endUser: string, addition: number): Promise<number> => { |