mirror of
https://github.com/d0zingcat/alert-message-center.git
synced 2026-06-08 07:26:46 +00:00
feat: add created_by/approved_by
Signed-off-by: d0zingcat <iamtangli42@gmail.com>
This commit is contained in:
@@ -32,6 +32,8 @@ api.get('/topics', requireAuth, async (c) => {
|
||||
const allTopics = await db.query.topics.findMany({
|
||||
where: eq(topics.status, 'approved'),
|
||||
with: {
|
||||
creator: true,
|
||||
approver: true,
|
||||
subscriptions: {
|
||||
where: (subscriptions, { eq }) =>
|
||||
isAdmin ? undefined : (currentUserId ? eq(subscriptions.userId, currentUserId) : undefined),
|
||||
@@ -59,6 +61,7 @@ api.get('/topics/all', requireAdmin, async (c) => {
|
||||
const allTopics = await db.query.topics.findMany({
|
||||
with: {
|
||||
creator: true,
|
||||
approver: true,
|
||||
subscriptions: true
|
||||
},
|
||||
orderBy: [desc(topics.createdAt)]
|
||||
@@ -71,14 +74,18 @@ api.get('/topics/my-requests', requireAuth, async (c) => {
|
||||
const requests = await db.query.topics.findMany({
|
||||
where: eq(topics.createdBy, session.id),
|
||||
orderBy: [desc(topics.createdAt)],
|
||||
with: {
|
||||
approver: true,
|
||||
}
|
||||
});
|
||||
return c.json(requests);
|
||||
});
|
||||
|
||||
api.post('/topics/:id/approve', requireAdmin, async (c) => {
|
||||
const id = c.req.param('id');
|
||||
const session = c.get('session');
|
||||
const result = await db.update(topics)
|
||||
.set({ status: 'approved' })
|
||||
.set({ status: 'approved', approvedBy: session.id })
|
||||
.where(eq(topics.id, id))
|
||||
.returning();
|
||||
return c.json(result[0]);
|
||||
@@ -105,6 +112,7 @@ api.post('/topics', requireAuth, zValidator('json', topicSchema), async (c) => {
|
||||
...body,
|
||||
status,
|
||||
createdBy: session.id,
|
||||
approvedBy: session.isAdmin ? session.id : null,
|
||||
}).returning();
|
||||
return c.json(result[0]);
|
||||
});
|
||||
|
||||
@@ -9,6 +9,7 @@ export const topics = pgTable('topics', {
|
||||
description: text('description'),
|
||||
status: text('status', { enum: ['pending', 'approved', 'rejected'] }).default('approved').notNull(),
|
||||
createdBy: text('created_by').references(() => users.id),
|
||||
approvedBy: text('approved_by').references(() => users.id),
|
||||
createdAt: timestamp('created_at').defaultNow().notNull(),
|
||||
});
|
||||
|
||||
@@ -17,6 +18,12 @@ export const topicsRelations = relations(topics, ({ many, one }) => ({
|
||||
creator: one(users, {
|
||||
fields: [topics.createdBy],
|
||||
references: [users.id],
|
||||
relationName: 'creator',
|
||||
}),
|
||||
approver: one(users, {
|
||||
fields: [topics.approvedBy],
|
||||
references: [users.id],
|
||||
relationName: 'approver',
|
||||
}),
|
||||
}));
|
||||
|
||||
@@ -31,6 +38,8 @@ export const users = pgTable('users', {
|
||||
|
||||
export const usersRelations = relations(users, ({ many }) => ({
|
||||
subscriptions: many(subscriptions),
|
||||
createdTopics: many(topics, { relationName: 'creator' }),
|
||||
approvedTopics: many(topics, { relationName: 'approver' }),
|
||||
}));
|
||||
|
||||
// Subscriptions: 用户直接订阅 Topic
|
||||
|
||||
Reference in New Issue
Block a user