rename
This commit is contained in:
@@ -11,7 +11,7 @@ import { postHistoryRouter } from "./routers/post-history";
|
|||||||
import { infillRouter } from "./routers/infill";
|
import { infillRouter } from "./routers/infill";
|
||||||
import { accountRouter } from "./routers/account";
|
import { accountRouter } from "./routers/account";
|
||||||
import { downloadsRouter } from "./routers/downloads";
|
import { downloadsRouter } from "./routers/downloads";
|
||||||
import { remoteDbRouter } from "./routers/remote-db";
|
import { cairnDbRouter } from "./routers/cairn";
|
||||||
import { appleNotificationsRouter } from "./routers/apple-notifications";
|
import { appleNotificationsRouter } from "./routers/apple-notifications";
|
||||||
import { createTRPCRouter, createTRPCContext } from "./utils";
|
import { createTRPCRouter, createTRPCContext } from "./utils";
|
||||||
import type { H3Event } from "h3";
|
import type { H3Event } from "h3";
|
||||||
@@ -30,7 +30,7 @@ export const appRouter = createTRPCRouter({
|
|||||||
infill: infillRouter,
|
infill: infillRouter,
|
||||||
account: accountRouter,
|
account: accountRouter,
|
||||||
downloads: downloadsRouter,
|
downloads: downloadsRouter,
|
||||||
remoteDb: remoteDbRouter,
|
cairnDb: cairnDbRouter,
|
||||||
appleNotifications: appleNotificationsRouter
|
appleNotifications: appleNotificationsRouter
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -171,7 +171,7 @@ const bulkSchema = z.object({
|
|||||||
authProviders: z.array(providerSchema).optional()
|
authProviders: z.array(providerSchema).optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const remoteDbRouter = createTRPCRouter({
|
export const cairnDbRouter = createTRPCRouter({
|
||||||
health: cairnProcedure.query(async () => {
|
health: cairnProcedure.query(async () => {
|
||||||
try {
|
try {
|
||||||
const conn = CairnConnectionFactory();
|
const conn = CairnConnectionFactory();
|
||||||
@@ -1300,40 +1300,6 @@ export const remoteDbRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
bulkUpsert: cairnProcedure
|
|
||||||
.input(bulkSchema)
|
|
||||||
.mutation(async ({ input, ctx }) => {
|
|
||||||
try {
|
|
||||||
const conn = CairnConnectionFactory();
|
|
||||||
|
|
||||||
if (input.users?.length) {
|
|
||||||
for (const user of input.users) {
|
|
||||||
if (user.id !== ctx.cairnUserId) {
|
|
||||||
throw new TRPCError({
|
|
||||||
code: "FORBIDDEN",
|
|
||||||
message: "User mismatch"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await conn.execute({
|
|
||||||
sql: `INSERT INTO users (id, email, emailVerified, firstName, lastName, displayName, avatarUrl, provider, appleUserId, status)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
||||||
ON CONFLICT(id) DO UPDATE SET email = excluded.email, emailVerified = excluded.emailVerified, firstName = excluded.firstName, lastName = excluded.lastName, displayName = excluded.displayName, avatarUrl = excluded.avatarUrl, provider = excluded.provider, appleUserId = excluded.appleUserId, status = excluded.status, updatedAt = datetime('now')`,
|
|
||||||
args: [
|
|
||||||
user.id,
|
|
||||||
user.email ?? null,
|
|
||||||
user.emailVerified ?? 0,
|
|
||||||
user.firstName ?? null,
|
|
||||||
user.lastName ?? null,
|
|
||||||
user.displayName ?? null,
|
|
||||||
user.avatarUrl ?? null,
|
|
||||||
user.provider ?? null,
|
|
||||||
user.appleUserId ?? null,
|
|
||||||
user.status ?? "active"
|
|
||||||
]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bulkUpsert: cairnProcedure
|
bulkUpsert: cairnProcedure
|
||||||
.input(bulkSchema)
|
.input(bulkSchema)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
@@ -1684,16 +1650,6 @@ export const remoteDbRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: true };
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed bulk upsert:", error);
|
|
||||||
throw new TRPCError({
|
|
||||||
code: "INTERNAL_SERVER_ERROR",
|
|
||||||
message: "Failed to bulk upsert"
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed bulk upsert:", error);
|
console.error("Failed bulk upsert:", error);
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
import { describe, it, expect, vi } from "vitest";
|
|
||||||
import { appRouter } from "~/server/api/root";
|
|
||||||
import { createTRPCContext } from "~/server/api/utils";
|
|
||||||
|
|
||||||
vi.mock("~/server/database", () => ({
|
|
||||||
CairnConnectionFactory: () => ({
|
|
||||||
execute: async () => ({ rows: [{ id: "1", email: "test@cairn.app" }] })
|
|
||||||
})
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("~/server/cache", () => ({
|
|
||||||
cache: {
|
|
||||||
get: async () => null,
|
|
||||||
set: async () => undefined
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("~/server/session-helpers", () => ({
|
|
||||||
getAuthSession: async () => ({ userId: "admin", isAdmin: true })
|
|
||||||
}));
|
|
||||||
|
|
||||||
describe("remoteDb router", () => {
|
|
||||||
it("returns users from remote database", async () => {
|
|
||||||
const caller = appRouter.createCaller(
|
|
||||||
await createTRPCContext({ nativeEvent: { node: { req: {} } } } as any)
|
|
||||||
);
|
|
||||||
|
|
||||||
const result = await caller.remoteDb.getUsers.query({ limit: 1, offset: 0 });
|
|
||||||
|
|
||||||
expect(result.users.length).toBe(1);
|
|
||||||
expect(result.users[0].email).toBe("test@cairn.app");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user