First half of Task-multi-email-identity. Lays down everything except the NextAuth callback wiring, which is gated on a research subagent finishing its survey of OAuth provider behavior for the email_verified claim across GitHub, Google, and Authentik. Schema (packages/database): * New user_email_identities table colocated with `users` in users.ts. Columns: id, user_id (FK), email (lowercased), verified_at, source, created_at, last_used_at. * Indexes: user_id, email, unique(user_id, email), and a PARTIAL unique index on email WHERE verified_at IS NOT NULL — a verified email resolves to exactly one users row globally, while unverified rows (none today; placeholder for the manual-verification follow-up) do not share the constraint. * Drizzle relation: users.emailIdentities -> userEmailIdentities, and the inverse one(users) relation. * Migration 0005 generated by db:generate, augmented with a backfill INSERT that seeds one source='primary' identity per existing users row using created_at as verified_at. Migration applied to dev DB; existing admin@tasks.dev user verified as 1:1 mapped. Server (apps/web/server): * apps/web/server/lib/identity.ts exports two pure read helpers: - userOwnsEmail(userId, email): boolean used by the (upcoming) invite-accept procedure to verify the human controls the invited address under any of their linked identities. - findUserIdByVerifiedEmail(email): the replacement for the old ensureUserIdByEmail lookup. Will be called from auth.ts once the OAuth research subagent returns. * apps/web/server/routers/identity.ts exposes identity.listMine — a protected procedure returning the caller's identities ordered by verifiedAt desc. Cross-user identity surface is intentionally NOT exposed here; that lives behind the workspace-scoped autocomplete in Task 3 with its own tenancy fence. UI (apps/web/app): * New route /[workspaceSlug]/settings/profile renders a read-only "Linked emails" section with per-identity row (email, source badge, verified state, last-used relative time) plus a hint that explains how to add another email (sign in via that email's OAuth provider). * Empty / loading / error states all handled. The "no identities" branch should never fire post-backfill but renders a friendly message instead of throwing. What's NOT in this commit: * auth.ts changes (ensureUserIdByEmail -> ensureUserIdByVerifiedEmail, OAuth callback identity upsert, cross-user conflict rejection). Waiting on subagent research to land the callback wiring correctly on the first try across all three providers. * Vitest tests. The pure helpers are 10-line query shims and the behavior-relevant assertion is the auth callback path — easier to write meaningful tests once that lands. All three CI gates green: pnpm lint (14 pre-existing warnings, unchanged), pnpm type-check (6/6 packages), pnpm test (14/14 existing tests across @tasks/shared, @tasks/database, @tasks/ai). Co-authored-by: Cursor <cursoragent@cursor.com>
226 lines
6.7 KiB
TypeScript
226 lines
6.7 KiB
TypeScript
import {
|
|
pgTable,
|
|
uuid,
|
|
varchar,
|
|
timestamp,
|
|
index,
|
|
uniqueIndex,
|
|
} from "drizzle-orm/pg-core";
|
|
import { relations } from "drizzle-orm";
|
|
import { objects, objectAssignees, workspaceMembers } from "./objects";
|
|
import { users, accounts, sessions, userEmailIdentities } from "./users";
|
|
import { propertyDefinitions } from "./properties";
|
|
import { propertyValues } from "./values";
|
|
import { views } from "./views";
|
|
import { templates } from "./templates";
|
|
import { objectTypeDefs } from "./types";
|
|
import { markdownBacklogItems } from "./markdown_backlog";
|
|
import { cursorSyncMappings } from "./cursor_sync";
|
|
import { workspaces } from "./workspaces";
|
|
|
|
export const objectRelations = pgTable(
|
|
"object_relations",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
sourceId: uuid("source_id")
|
|
.notNull()
|
|
.references(() => objects.id, { onDelete: "cascade" }),
|
|
targetId: uuid("target_id")
|
|
.notNull()
|
|
.references(() => objects.id, { onDelete: "cascade" }),
|
|
relationType: varchar("relation_type", { length: 50 }).notNull(),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
|
|
},
|
|
(table) => ({
|
|
sourceIdIdx: index("object_relations_source_id_idx").on(table.sourceId),
|
|
targetIdIdx: index("object_relations_target_id_idx").on(table.targetId),
|
|
relationTypeIdx: index("object_relations_relation_type_idx").on(table.relationType),
|
|
sourceTargetTypeUnique: uniqueIndex("object_relations_source_target_type_unique").on(
|
|
table.sourceId,
|
|
table.targetId,
|
|
table.relationType,
|
|
),
|
|
}),
|
|
);
|
|
|
|
// --- Drizzle ORM relations ---
|
|
|
|
export const usersRelations = relations(users, ({ many }) => ({
|
|
objectsCreated: many(objects),
|
|
workspaceMemberships: many(workspaceMembers),
|
|
ownedWorkspaces: many(workspaces),
|
|
objectAssignees: many(objectAssignees),
|
|
accounts: many(accounts),
|
|
sessions: many(sessions),
|
|
emailIdentities: many(userEmailIdentities),
|
|
}));
|
|
|
|
export const userEmailIdentitiesRelations = relations(
|
|
userEmailIdentities,
|
|
({ one }) => ({
|
|
user: one(users, {
|
|
fields: [userEmailIdentities.userId],
|
|
references: [users.id],
|
|
}),
|
|
}),
|
|
);
|
|
|
|
export const workspacesRelations = relations(workspaces, ({ one, many }) => ({
|
|
owner: one(users, {
|
|
fields: [workspaces.ownerUserId],
|
|
references: [users.id],
|
|
}),
|
|
members: many(workspaceMembers),
|
|
objects: many(objects),
|
|
templates: many(templates),
|
|
objectTypeDefs: many(objectTypeDefs),
|
|
propertyDefinitions: many(propertyDefinitions),
|
|
markdownBacklogItems: many(markdownBacklogItems),
|
|
}));
|
|
|
|
export const objectsRelations = relations(objects, ({ one, many }) => ({
|
|
parent: one(objects, {
|
|
fields: [objects.parentId],
|
|
references: [objects.id],
|
|
relationName: "objectHierarchy",
|
|
}),
|
|
children: many(objects, { relationName: "objectHierarchy" }),
|
|
workspace: one(workspaces, {
|
|
fields: [objects.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
template: one(templates, {
|
|
fields: [objects.templateId],
|
|
references: [templates.id],
|
|
}),
|
|
creator: one(users, {
|
|
fields: [objects.createdBy],
|
|
references: [users.id],
|
|
}),
|
|
propertyValues: many(propertyValues),
|
|
views: many(views),
|
|
assignees: many(objectAssignees),
|
|
outgoingRelations: many(objectRelations, { relationName: "relationSource" }),
|
|
incomingRelations: many(objectRelations, { relationName: "relationTarget" }),
|
|
}));
|
|
|
|
export const objectAssigneesRelations = relations(objectAssignees, ({ one }) => ({
|
|
object: one(objects, {
|
|
fields: [objectAssignees.objectId],
|
|
references: [objects.id],
|
|
}),
|
|
user: one(users, {
|
|
fields: [objectAssignees.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const workspaceMembersRelations = relations(workspaceMembers, ({ one }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [workspaceMembers.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
user: one(users, {
|
|
fields: [workspaceMembers.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const propertyDefinitionsRelations = relations(propertyDefinitions, ({ one, many }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [propertyDefinitions.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
values: many(propertyValues),
|
|
}));
|
|
|
|
export const propertyValuesRelations = relations(propertyValues, ({ one }) => ({
|
|
object: one(objects, {
|
|
fields: [propertyValues.objectId],
|
|
references: [objects.id],
|
|
}),
|
|
propertyDefinition: one(propertyDefinitions, {
|
|
fields: [propertyValues.propertyDefId],
|
|
references: [propertyDefinitions.id],
|
|
}),
|
|
}));
|
|
|
|
export const viewsRelations = relations(views, ({ one }) => ({
|
|
object: one(objects, {
|
|
fields: [views.objectId],
|
|
references: [objects.id],
|
|
}),
|
|
}));
|
|
|
|
export const templatesRelations = relations(templates, ({ one, many }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [templates.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
objects: many(objects),
|
|
}));
|
|
|
|
export const objectTypeDefsRelations = relations(objectTypeDefs, ({ one }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [objectTypeDefs.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
}));
|
|
|
|
export const accountsRelations = relations(accounts, ({ one }) => ({
|
|
user: one(users, {
|
|
fields: [accounts.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const sessionsRelations = relations(sessions, ({ one }) => ({
|
|
user: one(users, {
|
|
fields: [sessions.userId],
|
|
references: [users.id],
|
|
}),
|
|
}));
|
|
|
|
export const objectRelationsRelations = relations(objectRelations, ({ one }) => ({
|
|
source: one(objects, {
|
|
fields: [objectRelations.sourceId],
|
|
references: [objects.id],
|
|
relationName: "relationSource",
|
|
}),
|
|
target: one(objects, {
|
|
fields: [objectRelations.targetId],
|
|
references: [objects.id],
|
|
relationName: "relationTarget",
|
|
}),
|
|
}));
|
|
|
|
export const markdownBacklogItemsRelations = relations(
|
|
markdownBacklogItems,
|
|
({ one, many }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [markdownBacklogItems.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
parent: one(markdownBacklogItems, {
|
|
fields: [markdownBacklogItems.parentId],
|
|
references: [markdownBacklogItems.id],
|
|
relationName: "backlogHierarchy",
|
|
}),
|
|
children: many(markdownBacklogItems, { relationName: "backlogHierarchy" }),
|
|
cursorMapping: one(cursorSyncMappings, {
|
|
fields: [markdownBacklogItems.id],
|
|
references: [cursorSyncMappings.backlogItemId],
|
|
}),
|
|
}),
|
|
);
|
|
|
|
export const cursorSyncMappingsRelations = relations(cursorSyncMappings, ({ one }) => ({
|
|
workspace: one(workspaces, {
|
|
fields: [cursorSyncMappings.workspaceId],
|
|
references: [workspaces.id],
|
|
}),
|
|
backlogItem: one(markdownBacklogItems, {
|
|
fields: [cursorSyncMappings.backlogItemId],
|
|
references: [markdownBacklogItems.id],
|
|
}),
|
|
}));
|