move to webapp

This commit is contained in:
m5r
2021-07-18 23:32:45 +08:00
parent 61c23ec9a7
commit a989125e6e
167 changed files with 26607 additions and 24066 deletions

55
src/database/customer.ts Normal file
View File

@ -0,0 +1,55 @@
import appLogger from "../../lib/logger";
import supabase from "../supabase/server";
import { computeEncryptionKey } from "./_encryption";
const logger = appLogger.child({ module: "customer" });
export type Customer = {
id: string;
email: string;
name: string;
paddleCustomerId: string;
paddleSubscriptionId: string;
accountSid: string;
authToken: string;
encryptionKey: string;
};
type CreateCustomerParams = Pick<Customer, "id" | "email" | "name">;
export async function createCustomer({ id, email, name }: CreateCustomerParams): Promise<Customer> {
const encryptionKey = computeEncryptionKey(id).toString("hex");
const { error, data } = await supabase
.from<Customer>("customer")
.insert({
id,
email,
name,
encryptionKey,
});
if (error) throw error;
console.log("data", data);
return data![0];
}
export async function findCustomer(id: Customer["id"]): Promise<Customer> {
const { error, data } = await supabase
.from<Customer>("customer")
.select("*")
.eq("id", id)
.single();
if (error) throw error;
return data!;
}
export async function updateCustomer(id: string, update: Partial<Customer>) {
await supabase.from<Customer>("customer")
.update(update)
.eq("id", id)
.throwOnError();
}