Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,188 @@
|
||||
/**
|
||||
* Server-side helpers for the DeviceToken Prisma model (Phase 08 #65).
|
||||
*
|
||||
* The mobile-wrapper register-device endpoint upserts on the unique
|
||||
* `token` column, the push-worker job filters by `revokedAt IS NULL`
|
||||
* for fan-out, and the cron prune script revokes rows older than 90d.
|
||||
*
|
||||
* Keeping these functions narrow and prisma-shape-only lets unit tests
|
||||
* inject a stub client without pulling in `@prisma/client` at all.
|
||||
*/
|
||||
import { getDbClient, type DbClient } from "./client.server.js";
|
||||
|
||||
export type DevicePlatform = "ios" | "android" | "web";
|
||||
export type PushProvider = "fcm" | "apns" | "webpush";
|
||||
|
||||
const DEVICE_PLATFORMS = new Set<DevicePlatform>(["ios", "android", "web"]);
|
||||
const PUSH_PROVIDERS = new Set<PushProvider>(["fcm", "apns", "webpush"]);
|
||||
|
||||
export function parseDevicePlatform(value: unknown): DevicePlatform | null {
|
||||
return typeof value === "string" && DEVICE_PLATFORMS.has(value as DevicePlatform) ? (value as DevicePlatform) : null;
|
||||
}
|
||||
|
||||
export function parsePushProvider(value: unknown): PushProvider | null {
|
||||
return typeof value === "string" && PUSH_PROVIDERS.has(value as PushProvider) ? (value as PushProvider) : null;
|
||||
}
|
||||
|
||||
/** Default provider for a given platform when the client omits it. */
|
||||
export function defaultPushProviderFor(platform: DevicePlatform): PushProvider {
|
||||
if (platform === "ios") return "apns";
|
||||
if (platform === "android") return "fcm";
|
||||
return "webpush";
|
||||
}
|
||||
|
||||
export type DeviceTokenRecord = {
|
||||
id: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
userId: string;
|
||||
tenantId: string | null;
|
||||
platform: string;
|
||||
token: string;
|
||||
pushProvider: string;
|
||||
appVersion: string | null;
|
||||
lastSeenAt: Date;
|
||||
revokedAt: Date | null;
|
||||
};
|
||||
|
||||
export type DeviceTokenClient = {
|
||||
deviceToken: {
|
||||
upsert(args: {
|
||||
where: { token: string };
|
||||
create: {
|
||||
userId: string;
|
||||
tenantId: string | null;
|
||||
platform: DevicePlatform;
|
||||
token: string;
|
||||
pushProvider: PushProvider;
|
||||
appVersion?: string | null;
|
||||
};
|
||||
update: {
|
||||
userId: string;
|
||||
tenantId: string | null;
|
||||
platform: DevicePlatform;
|
||||
pushProvider: PushProvider;
|
||||
appVersion?: string | null;
|
||||
lastSeenAt: Date;
|
||||
revokedAt: null;
|
||||
};
|
||||
}): Promise<DeviceTokenRecord>;
|
||||
update(args: {
|
||||
where: { token: string };
|
||||
data: { revokedAt: Date };
|
||||
}): Promise<DeviceTokenRecord>;
|
||||
updateMany(args: {
|
||||
where: { userId: string; revokedAt: null };
|
||||
data: { revokedAt: Date };
|
||||
}): Promise<{ count: number }>;
|
||||
findMany(args: {
|
||||
where: { userId?: string; tenantId?: string; revokedAt?: null };
|
||||
orderBy?: { createdAt: "desc" | "asc" };
|
||||
take?: number;
|
||||
}): Promise<readonly DeviceTokenRecord[]>;
|
||||
findUnique(args: { where: { token: string } }): Promise<DeviceTokenRecord | null>;
|
||||
};
|
||||
};
|
||||
|
||||
export type RegisterDeviceTokenInput = {
|
||||
userId: string;
|
||||
tenantId: string | null;
|
||||
platform: DevicePlatform;
|
||||
token: string;
|
||||
pushProvider?: PushProvider;
|
||||
appVersion?: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Idempotent upsert keyed by `token`. Re-registration of the same token
|
||||
* (typical on app cold-start) just bumps `lastSeenAt` and clears any
|
||||
* prior `revokedAt`.
|
||||
*/
|
||||
export async function registerDeviceToken(
|
||||
input: RegisterDeviceTokenInput,
|
||||
client: DeviceTokenClient = getDbClient() as unknown as DeviceTokenClient,
|
||||
): Promise<DeviceTokenRecord> {
|
||||
const provider = input.pushProvider ?? defaultPushProviderFor(input.platform);
|
||||
const now = new Date();
|
||||
return client.deviceToken.upsert({
|
||||
where: { token: input.token },
|
||||
create: {
|
||||
userId: input.userId,
|
||||
tenantId: input.tenantId,
|
||||
platform: input.platform,
|
||||
token: input.token,
|
||||
pushProvider: provider,
|
||||
appVersion: input.appVersion ?? null,
|
||||
},
|
||||
update: {
|
||||
userId: input.userId,
|
||||
tenantId: input.tenantId,
|
||||
platform: input.platform,
|
||||
pushProvider: provider,
|
||||
appVersion: input.appVersion ?? null,
|
||||
lastSeenAt: now,
|
||||
revokedAt: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a single token revoked. Called when the push provider responds
|
||||
* with `Unregistered` / `InvalidRegistration` so the next fan-out pass
|
||||
* skips this token.
|
||||
*/
|
||||
export async function revokeDeviceToken(
|
||||
token: string,
|
||||
client: DeviceTokenClient = getDbClient() as unknown as DeviceTokenClient,
|
||||
): Promise<DeviceTokenRecord | null> {
|
||||
try {
|
||||
return await client.deviceToken.update({
|
||||
where: { token },
|
||||
data: { revokedAt: new Date() },
|
||||
});
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Revoke all active tokens for a user (logout / password-change hook). */
|
||||
export async function revokeDeviceTokensForUser(
|
||||
userId: string,
|
||||
client: DeviceTokenClient = getDbClient() as unknown as DeviceTokenClient,
|
||||
): Promise<number> {
|
||||
const { count } = await client.deviceToken.updateMany({
|
||||
where: { userId, revokedAt: null },
|
||||
data: { revokedAt: new Date() },
|
||||
});
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* List active tokens for fan-out. Scoped by user (always required) plus
|
||||
* optional tenant. Returns at most `take` tokens, newest first.
|
||||
*/
|
||||
export async function listActiveDeviceTokens(
|
||||
args: { userId: string; tenantId?: string; take?: number },
|
||||
client: DeviceTokenClient = getDbClient() as unknown as DeviceTokenClient,
|
||||
): Promise<readonly DeviceTokenRecord[]> {
|
||||
return client.deviceToken.findMany({
|
||||
where: {
|
||||
userId: args.userId,
|
||||
...(args.tenantId ? { tenantId: args.tenantId } : {}),
|
||||
revokedAt: null,
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: args.take ?? 100,
|
||||
});
|
||||
}
|
||||
|
||||
export async function findDeviceTokenByToken(
|
||||
token: string,
|
||||
client: DeviceTokenClient = getDbClient() as unknown as DeviceTokenClient,
|
||||
): Promise<DeviceTokenRecord | null> {
|
||||
return client.deviceToken.findUnique({ where: { token } });
|
||||
}
|
||||
|
||||
// Re-export DbClient so call-sites in the API / worker can keep their
|
||||
// type assertion narrow without needing an extra import path.
|
||||
export type { DbClient };
|
||||
Reference in new issue
Block a user