105 lines
2.8 KiB
TypeScript
105 lines
2.8 KiB
TypeScript
import { internalMutation, internalQuery } from "../_generated/server";
|
|
import { v } from "convex/values";
|
|
|
|
export const getSession = internalQuery({
|
|
args: { userId: v.id("users") },
|
|
returns: v.union(
|
|
v.object({
|
|
_id: v.id("comdirectSessions"),
|
|
sessionUuid: v.string(),
|
|
identifier: v.optional(v.string()),
|
|
accessToken: v.optional(v.string()),
|
|
refreshToken: v.optional(v.string()),
|
|
secondaryActive: v.boolean(),
|
|
challengeId: v.optional(v.string()),
|
|
challengeType: v.optional(v.string()),
|
|
status: v.string(),
|
|
}),
|
|
v.null(),
|
|
),
|
|
handler: async (ctx, args) => {
|
|
return await ctx.db
|
|
.query("comdirectSessions")
|
|
.withIndex("by_user", (q) => q.eq("userId", args.userId))
|
|
.unique();
|
|
},
|
|
});
|
|
|
|
export const upsertSession = internalMutation({
|
|
args: {
|
|
userId: v.id("users"),
|
|
sessionUuid: v.string(),
|
|
identifier: v.optional(v.string()),
|
|
accessToken: v.optional(v.string()),
|
|
refreshToken: v.optional(v.string()),
|
|
secondaryActive: v.boolean(),
|
|
challengeId: v.optional(v.string()),
|
|
challengeType: v.optional(v.string()),
|
|
status: v.string(),
|
|
expiresAt: v.optional(v.number()),
|
|
},
|
|
returns: v.id("comdirectSessions"),
|
|
handler: async (ctx, args) => {
|
|
const existing = await ctx.db
|
|
.query("comdirectSessions")
|
|
.withIndex("by_user", (q) => q.eq("userId", args.userId))
|
|
.unique();
|
|
if (existing) {
|
|
await ctx.db.patch(existing._id, args);
|
|
return existing._id;
|
|
}
|
|
return await ctx.db.insert("comdirectSessions", args);
|
|
},
|
|
});
|
|
|
|
export const clearSession = internalMutation({
|
|
args: { userId: v.id("users") },
|
|
returns: v.null(),
|
|
handler: async (ctx, args) => {
|
|
const existing = await ctx.db
|
|
.query("comdirectSessions")
|
|
.withIndex("by_user", (q) => q.eq("userId", args.userId))
|
|
.unique();
|
|
if (existing) {
|
|
await ctx.db.delete(existing._id);
|
|
}
|
|
return null;
|
|
},
|
|
});
|
|
|
|
export const upsertAccountFromComdirect = internalMutation({
|
|
args: {
|
|
userId: v.id("users"),
|
|
externalId: v.string(),
|
|
name: v.string(),
|
|
iban: v.optional(v.string()),
|
|
balance: v.number(),
|
|
},
|
|
returns: v.id("accounts"),
|
|
handler: async (ctx, args) => {
|
|
const existing = await ctx.db
|
|
.query("accounts")
|
|
.withIndex("by_user_external", (q) =>
|
|
q.eq("userId", args.userId).eq("externalId", args.externalId),
|
|
)
|
|
.unique();
|
|
if (existing) {
|
|
await ctx.db.patch(existing._id, {
|
|
name: args.name,
|
|
iban: args.iban,
|
|
});
|
|
return existing._id;
|
|
}
|
|
return await ctx.db.insert("accounts", {
|
|
userId: args.userId,
|
|
name: args.name,
|
|
type: "giro",
|
|
iban: args.iban,
|
|
openingBalance: args.balance,
|
|
currency: "EUR",
|
|
isArchived: false,
|
|
externalId: args.externalId,
|
|
});
|
|
},
|
|
});
|