-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathadd.ts
46 lines (39 loc) · 1.09 KB
/
add.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { MessageAllowanceAdd, DbAllowance } from "~/types";
import db from "../../db";
const add = async (message: MessageAllowanceAdd) => {
const host = message.args.host;
const name = message.args.name;
const imageURL = message.args.imageURL;
const totalBudget = message.args.totalBudget;
const allowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();
if (allowance) {
if (!allowance.id) return { error: "id is missing" };
await db.allowances.update(allowance.id, {
enabled: true,
imageURL: imageURL,
name: name,
remainingBudget: totalBudget,
totalBudget: totalBudget,
});
} else {
const dbAllowance: DbAllowance = {
createdAt: Date.now().toString(),
enabled: true,
host: host,
imageURL: imageURL,
lastPaymentAt: 0,
lnurlAuth: false,
name: name,
remainingBudget: totalBudget,
tag: "",
totalBudget: totalBudget,
};
await db.allowances.add(dbAllowance);
}
await db.saveToStorage();
return { data: { allowance } };
};
export default add;