summaryrefslogtreecommitdiff
path: root/db/schema/notification.ts
blob: 8b745ac2f810d4554e228271d93a9c2a51ddcf97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// db/schema/notifications.ts
import { 
    pgTable, 
    uuid, 
    varchar, 
    text, 
    boolean, 
    timestamp, 
    index 
  } from 'drizzle-orm/pg-core';
  import { users } from './users'; // 기존 users 테이블
  import { relations } from 'drizzle-orm';
  
  export const notifications = pgTable('notifications', {
    id: uuid('id').primaryKey().defaultRandom(),
    userId: varchar('user_id', { length: 255 }).notNull(),
    title: varchar('title', { length: 255 }).notNull(),
    message: text('message').notNull(),
    type: varchar('type', { length: 50 }).notNull(), // 'assignment', 'update', 'reminder' 등
    relatedRecordId: varchar('related_record_id', { length: 255 }),
    relatedRecordType: varchar('related_record_type', { length: 100 }), // 'project', 'task', 'order' 등
    isRead: boolean('is_read').default(false).notNull(),
    createdAt: timestamp('created_at').defaultNow().notNull(),
    readAt: timestamp('read_at'),
  }, (table) => ({
    userIdIdx: index('idx_notifications_user_id').on(table.userId),
    createdAtIdx: index('idx_notifications_created_at').on(table.createdAt.desc()),
    isReadIdx: index('idx_notifications_is_read').on(table.isRead),
    userReadIdx: index('idx_notifications_user_read').on(table.userId, table.isRead),
  }));
  
  // 관계 정의
  export const notificationsRelations = relations(notifications, ({ one }) => ({
    user: one(users, {
      fields: [notifications.userId],
      references: [users.id],
    }),
  }));
  
  // 타입 정의
  export type Notification = typeof notifications.$inferSelect;
  export type NewNotification = typeof notifications.$inferInsert;
  
  // 알림 타입 enum
  export const NotificationType = {
    ASSIGNMENT: 'assignment',
    UPDATE: 'update', 
    REMINDER: 'reminder',
    APPROVAL: 'approval',
    DEADLINE: 'deadline',
    STATUS_CHANGE: 'status_change'
  } as const;
  
  export type NotificationTypeValue = typeof NotificationType[keyof typeof NotificationType];
  
  // 관련 레코드 타입 enum
  export const RelatedRecordType = {
    PROJECT: 'project',
    TASK: 'task', 
    ORDER: 'order',
    DOCUMENT: 'document',
    USER: 'user'
  } as const;
  
  export type RelatedRecordTypeValue = typeof RelatedRecordType[keyof typeof RelatedRecordType];