395 lines
16 KiB
TypeScript
395 lines
16 KiB
TypeScript
import { pgTable, text, integer, bigint, numeric, jsonb, timestamp, uniqueIndex, index, uuid, json, boolean } from 'drizzle-orm/pg-core';
|
|
import { relations } from 'drizzle-orm';
|
|
|
|
export const polls = pgTable('polls', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
creatorFid: integer('creator_fid').notNull(),
|
|
creatorAddress: text('creator_address').notNull(),
|
|
question: text('question').notNull(),
|
|
description: text('description'),
|
|
emoji: text('emoji'),
|
|
startTime: integer('start_time').notNull(),
|
|
endTime: integer('end_time').notNull(),
|
|
status: text('status').notNull().default('active'),
|
|
|
|
// Verification
|
|
verificationType: text('verification_type').notNull(),
|
|
verificationConfig: json('verification_config').notNull(),
|
|
correctOptionId: uuid('correct_option_id'),
|
|
verifiedAt: integer('verified_at'),
|
|
verificationProof: text('verification_proof'),
|
|
|
|
// Money
|
|
totalPot: numeric('total_pot').notNull().default('0'),
|
|
protocolFee: numeric('protocol_fee').notNull().default('0'),
|
|
creationFee: numeric('creation_fee').notNull().default('0'),
|
|
winnersCount: integer('winners_count').default(0),
|
|
distributedAt: integer('distributed_at'),
|
|
distributionTxHashes: json('distribution_tx_hashes'), // Store as JSON array
|
|
|
|
createdAt: integer('created_at').notNull(),
|
|
updatedAt: timestamp('updated_at').defaultNow(),
|
|
}, (table) => {
|
|
return {
|
|
statusIdx: index('idx_polls_status').on(table.status),
|
|
endTimeIdx: index('idx_polls_end_time').on(table.endTime),
|
|
};
|
|
});
|
|
|
|
export const pollOptions = pgTable('poll_options', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
pollId: uuid('poll_id').references(() => polls.id, { onDelete: 'cascade' }).notNull(),
|
|
text: text('text').notNull(),
|
|
emoji: text('emoji'),
|
|
votes: integer('votes').default(0),
|
|
totalAmount: numeric('total_amount', { precision: 20, scale: 8 }).default('0'),
|
|
position: integer('position').notNull(),
|
|
});
|
|
|
|
export const votes = pgTable('votes', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
pollId: uuid('poll_id').references(() => polls.id, { onDelete: 'cascade' }).notNull(),
|
|
userFid: text('user_fid').notNull(),
|
|
userAddress: text('user_address').notNull(),
|
|
optionId: uuid('option_id').references(() => pollOptions.id, { onDelete: 'cascade' }).notNull(),
|
|
amount: numeric('amount', { precision: 20, scale: 8 }).notNull(),
|
|
txHash: text('tx_hash').notNull(),
|
|
timestamp: bigint('timestamp', { mode: 'number' }).notNull(),
|
|
|
|
createdAt: timestamp('created_at').defaultNow(),
|
|
}, (table) => {
|
|
return {
|
|
pollIdIdx: index('idx_votes_poll_id').on(table.pollId),
|
|
userFidIdx: index('idx_votes_user_fid').on(table.userFid),
|
|
uniqueVote: uniqueIndex('unique_vote_per_poll').on(table.pollId, table.userFid),
|
|
};
|
|
});
|
|
|
|
export const distributions = pgTable('distributions', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
pollId: uuid('poll_id').references(() => polls.id, { onDelete: 'cascade' }).notNull(),
|
|
userFid: text('user_fid').notNull(),
|
|
userAddress: text('user_address').notNull(),
|
|
amount: numeric('amount', { precision: 20, scale: 8 }).notNull(),
|
|
txHash: text('tx_hash').notNull(),
|
|
timestamp: bigint('timestamp', { mode: 'number' }).notNull(),
|
|
|
|
createdAt: timestamp('created_at').defaultNow(),
|
|
});
|
|
|
|
// ============= SOCIAL FEATURES =============
|
|
|
|
export const comments = pgTable('comments', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
pollId: uuid('poll_id').references(() => polls.id, { onDelete: 'cascade' }).notNull(),
|
|
userFid: text('user_fid').notNull(),
|
|
userAddress: text('user_address').notNull(),
|
|
content: text('content').notNull(),
|
|
parentId: uuid('parent_id').references((): any => comments.id, { onDelete: 'cascade' }),
|
|
likesCount: integer('likes_count').default(0).notNull(),
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
updatedAt: timestamp('updated_at').defaultNow().notNull(),
|
|
isDeleted: boolean('is_deleted').default(false).notNull(),
|
|
}, (table) => {
|
|
return {
|
|
pollIdIdx: index('idx_comments_poll_id').on(table.pollId),
|
|
userFidIdx: index('idx_comments_user_fid').on(table.userFid),
|
|
parentIdIdx: index('idx_comments_parent_id').on(table.parentId),
|
|
};
|
|
});
|
|
|
|
export const commentLikes = pgTable('comment_likes', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
commentId: uuid('comment_id').references(() => comments.id, { onDelete: 'cascade' }).notNull(),
|
|
userFid: text('user_fid').notNull(),
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
}, (table) => {
|
|
return {
|
|
uniqueLike: uniqueIndex('unique_comment_like').on(table.commentId, table.userFid),
|
|
};
|
|
});
|
|
|
|
export const follows = pgTable('follows', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
followerFid: text('follower_fid').notNull(),
|
|
followingFid: text('following_fid').notNull(),
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
}, (table) => {
|
|
return {
|
|
followerIdx: index('idx_follows_follower').on(table.followerFid),
|
|
followingIdx: index('idx_follows_following').on(table.followingFid),
|
|
uniqueFollow: uniqueIndex('unique_follow').on(table.followerFid, table.followingFid),
|
|
};
|
|
});
|
|
|
|
export const notifications = pgTable('notifications', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
userFid: text('user_fid').notNull(),
|
|
type: text('type').notNull(), // 'vote' | 'comment' | 'like' | 'follow' | 'win' | 'mention' | 'poll_closed' | 'achievement'
|
|
title: text('title').notNull(),
|
|
message: text('message').notNull(),
|
|
link: text('link'),
|
|
isRead: boolean('is_read').default(false).notNull(),
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
metadata: jsonb('metadata'),
|
|
}, (table) => {
|
|
return {
|
|
userIdxUnread: index('idx_notifications_user_unread').on(table.userFid, table.isRead),
|
|
createdAtIdx: index('idx_notifications_created_at').on(table.createdAt),
|
|
};
|
|
});
|
|
|
|
export const userSettings = pgTable('user_settings', {
|
|
userFid: text('user_fid').primaryKey(),
|
|
notifyOnComment: boolean('notify_on_comment').default(true).notNull(),
|
|
notifyOnLike: boolean('notify_on_like').default(true).notNull(),
|
|
notifyOnFollow: boolean('notify_on_follow').default(true).notNull(),
|
|
notifyOnWin: boolean('notify_on_win').default(true).notNull(),
|
|
notifyOnMention: boolean('notify_on_mention').default(true).notNull(),
|
|
displayName: text('display_name'),
|
|
bio: text('bio'),
|
|
updatedAt: timestamp('updated_at').defaultNow().notNull(),
|
|
});
|
|
|
|
export const achievements = pgTable('achievements', {
|
|
id: text('id').primaryKey(),
|
|
name: text('name').notNull(),
|
|
description: text('description').notNull(),
|
|
icon: text('icon').notNull(),
|
|
category: text('category').notNull(), // 'voting' | 'winning' | 'social' | 'streak' | 'creation'
|
|
requirement: jsonb('requirement').notNull(),
|
|
});
|
|
|
|
export const userAchievements = pgTable('user_achievements', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
userFid: text('user_fid').notNull(),
|
|
achievementId: text('achievement_id').references(() => achievements.id, { onDelete: 'cascade' }).notNull(),
|
|
earnedAt: timestamp('earned_at').defaultNow().notNull(),
|
|
progress: integer('progress').default(0).notNull(),
|
|
}, (table) => {
|
|
return {
|
|
userFidIdx: index('idx_user_achievements_user').on(table.userFid),
|
|
uniqueAchievement: uniqueIndex('unique_user_achievement').on(table.userFid, table.achievementId),
|
|
};
|
|
});
|
|
|
|
export const reports = pgTable('reports', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
reporterFid: text('reporter_fid').notNull(),
|
|
resourceType: text('resource_type').notNull(), // 'poll' | 'comment' | 'user'
|
|
resourceId: text('resource_id').notNull(),
|
|
reason: text('reason').notNull(),
|
|
description: text('description'),
|
|
status: text('status').default('pending').notNull(), // 'pending' | 'resolved' | 'dismissed'
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
resolvedAt: timestamp('resolved_at'),
|
|
resolvedBy: text('resolved_by'),
|
|
}, (table) => {
|
|
return {
|
|
statusIdx: index('idx_reports_status').on(table.status),
|
|
resourceIdx: index('idx_reports_resource').on(table.resourceType, table.resourceId),
|
|
};
|
|
});
|
|
|
|
export const bookmarks = pgTable('bookmarks', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
userFid: text('user_fid').notNull(),
|
|
pollId: uuid('poll_id').references(() => polls.id, { onDelete: 'cascade' }).notNull(),
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
}, (table) => {
|
|
return {
|
|
userFidIdx: index('idx_bookmarks_user').on(table.userFid),
|
|
uniqueBookmark: uniqueIndex('unique_bookmark').on(table.userFid, table.pollId),
|
|
};
|
|
});
|
|
|
|
export const pollShares = pgTable('poll_shares', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
pollId: uuid('poll_id').references(() => polls.id, { onDelete: 'cascade' }).notNull(),
|
|
userFid: text('user_fid').notNull(),
|
|
platform: text('platform').notNull(), // 'warpcast' | 'twitter' | 'link'
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
}, (table) => {
|
|
return {
|
|
pollIdIdx: index('idx_poll_shares_poll').on(table.pollId),
|
|
};
|
|
});
|
|
|
|
// Relations
|
|
export const pollsRelations = relations(polls, ({ many }) => ({
|
|
options: many(pollOptions),
|
|
votes: many(votes),
|
|
distributions: many(distributions),
|
|
}));
|
|
|
|
export const pollOptionsRelations = relations(pollOptions, ({ one }) => ({
|
|
poll: one(polls, {
|
|
fields: [pollOptions.pollId],
|
|
references: [polls.id],
|
|
}),
|
|
}));
|
|
|
|
export const votesRelations = relations(votes, ({ one }) => ({
|
|
poll: one(polls, {
|
|
fields: [votes.pollId],
|
|
references: [polls.id],
|
|
}),
|
|
option: one(pollOptions, {
|
|
fields: [votes.optionId],
|
|
references: [pollOptions.id],
|
|
}),
|
|
}));
|
|
|
|
export const commentsRelations = relations(comments, ({ one, many }) => ({
|
|
poll: one(polls, {
|
|
fields: [comments.pollId],
|
|
references: [polls.id],
|
|
}),
|
|
parent: one(comments, {
|
|
fields: [comments.parentId],
|
|
references: [comments.id],
|
|
}),
|
|
replies: many(comments),
|
|
likes: many(commentLikes),
|
|
}));
|
|
|
|
export const commentLikesRelations = relations(commentLikes, ({ one }) => ({
|
|
comment: one(comments, {
|
|
fields: [commentLikes.commentId],
|
|
references: [comments.id],
|
|
}),
|
|
}));
|
|
|
|
export const bookmarksRelations = relations(bookmarks, ({ one }) => ({
|
|
poll: one(polls, {
|
|
fields: [bookmarks.pollId],
|
|
references: [polls.id],
|
|
}),
|
|
}));
|
|
|
|
export const userAchievementsRelations = relations(userAchievements, ({ one }) => ({
|
|
achievement: one(achievements, {
|
|
fields: [userAchievements.achievementId],
|
|
references: [achievements.id],
|
|
}),
|
|
}));
|
|
|
|
// ============= NEW FEATURES =============
|
|
|
|
export const challenges = pgTable('challenges', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
challengerFid: text('challenger_fid').notNull(),
|
|
opponentFid: text('opponent_fid').notNull(),
|
|
status: text('status').default('active').notNull(), // 'active' | 'completed' | 'cancelled'
|
|
challengerScore: integer('challenger_score').default(0).notNull(),
|
|
opponentScore: integer('opponent_score').default(0).notNull(),
|
|
totalPolls: integer('total_polls').default(5).notNull(),
|
|
winnerFid: text('winner_fid'),
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
completedAt: timestamp('completed_at'),
|
|
}, (table) => {
|
|
return {
|
|
challengerIdx: index('idx_challenges_challenger').on(table.challengerFid),
|
|
opponentIdx: index('idx_challenges_opponent').on(table.opponentFid),
|
|
statusIdx: index('idx_challenges_status').on(table.status),
|
|
};
|
|
});
|
|
|
|
export const chatMessages = pgTable('chat_messages', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
pollId: uuid('poll_id').references(() => polls.id, { onDelete: 'cascade' }).notNull(),
|
|
userFid: text('user_fid').notNull(),
|
|
message: text('message').notNull(),
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
}, (table) => {
|
|
return {
|
|
pollIdIdx: index('idx_chat_messages_poll').on(table.pollId),
|
|
createdAtIdx: index('idx_chat_messages_created').on(table.createdAt),
|
|
};
|
|
});
|
|
|
|
export const referrals = pgTable('referrals', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
code: text('code').notNull().unique(),
|
|
referrerFid: text('referrer_fid').notNull(),
|
|
referredFid: text('referred_fid'),
|
|
status: text('status').default('pending').notNull(), // 'pending' | 'completed' | 'rewarded'
|
|
rewardAmount: numeric('reward_amount', { precision: 20, scale: 8 }).default('0.01'),
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
completedAt: timestamp('completed_at'),
|
|
}, (table) => {
|
|
return {
|
|
codeIdx: index('idx_referrals_code').on(table.code),
|
|
referrerIdx: index('idx_referrals_referrer').on(table.referrerFid),
|
|
referredIdx: index('idx_referrals_referred').on(table.referredFid),
|
|
};
|
|
});
|
|
|
|
export const creatorEarnings = pgTable('creator_earnings', {
|
|
id: uuid('id').primaryKey().defaultRandom(),
|
|
creatorFid: text('creator_fid').notNull(),
|
|
pollId: uuid('poll_id').references(() => polls.id, { onDelete: 'cascade' }).notNull(),
|
|
amount: numeric('amount', { precision: 20, scale: 8 }).notNull(),
|
|
status: text('status').default('pending').notNull(), // 'pending' | 'paid'
|
|
paidAt: timestamp('paid_at'),
|
|
txHash: text('tx_hash'),
|
|
createdAt: timestamp('created_at').defaultNow().notNull(),
|
|
}, (table) => {
|
|
return {
|
|
creatorIdx: index('idx_creator_earnings_creator').on(table.creatorFid),
|
|
statusIdx: index('idx_creator_earnings_status').on(table.status),
|
|
};
|
|
});
|
|
|
|
export const userStats = pgTable('user_stats', {
|
|
userFid: text('user_fid').primaryKey(),
|
|
totalVotes: integer('total_votes').default(0).notNull(),
|
|
totalWins: integer('total_wins').default(0).notNull(),
|
|
totalSpent: numeric('total_spent', { precision: 20, scale: 8 }).default('0').notNull(),
|
|
totalEarned: numeric('total_earned', { precision: 20, scale: 8 }).default('0').notNull(),
|
|
currentStreak: integer('current_streak').default(0).notNull(),
|
|
longestStreak: integer('longest_streak').default(0).notNull(),
|
|
lastVoteDate: timestamp('last_vote_date'),
|
|
pollsCreated: integer('polls_created').default(0).notNull(),
|
|
commentsCount: integer('comments_count').default(0).notNull(),
|
|
followersCount: integer('followers_count').default(0).notNull(),
|
|
followingCount: integer('following_count').default(0).notNull(),
|
|
updatedAt: timestamp('updated_at').defaultNow().notNull(),
|
|
});
|
|
|
|
export const challengesRelations = relations(challenges, ({ one }) => ({
|
|
challenger: one(userStats, {
|
|
fields: [challenges.challengerFid],
|
|
references: [userStats.userFid],
|
|
}),
|
|
opponent: one(userStats, {
|
|
fields: [challenges.opponentFid],
|
|
references: [userStats.userFid],
|
|
}),
|
|
}));
|
|
|
|
export const chatMessagesRelations = relations(chatMessages, ({ one }) => ({
|
|
poll: one(polls, {
|
|
fields: [chatMessages.pollId],
|
|
references: [polls.id],
|
|
}),
|
|
}));
|
|
|
|
export const referralsRelations = relations(referrals, ({ one }) => ({
|
|
referrer: one(userStats, {
|
|
fields: [referrals.referrerFid],
|
|
references: [userStats.userFid],
|
|
}),
|
|
}));
|
|
|
|
export const creatorEarningsRelations = relations(creatorEarnings, ({ one }) => ({
|
|
creator: one(userStats, {
|
|
fields: [creatorEarnings.creatorFid],
|
|
references: [userStats.userFid],
|
|
}),
|
|
poll: one(polls, {
|
|
fields: [creatorEarnings.pollId],
|
|
references: [polls.id],
|
|
}),
|
|
}));
|