MCPcopy Index your code
hub / github.com/sqlchat/sqlchat / addUsage

Function addUsage

src/pages/api/utils/usage.ts:26–59  ·  view source on GitHub ↗
(endUser: string, addition: number)

Source from the content-addressed store, hash-verified

24
25// We coerce individual usage to the begining of the day to reduce the usage records.
26export const addUsage = async (endUser: string, addition: number): Promise<number> => {
27 const now = new Date();
28 const today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
29 const usage = await prisma.usage.findFirst({
30 where: {
31 endUser: endUser,
32 createdAt: today,
33 },
34 });
35
36 let newUsage = 0;
37 if (usage) {
38 newUsage = usage.count + addition;
39 await prisma.usage.update({
40 where: {
41 id: usage.id,
42 },
43 data: {
44 count: newUsage,
45 },
46 });
47 } else {
48 newUsage = addition;
49 await prisma.usage.create({
50 data: {
51 endUser: endUser,
52 createdAt: today,
53 count: newUsage,
54 },
55 });
56 }
57
58 return newUsage;
59};

Callers 1

handlerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected