Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin endpoint to get latest site comments #110

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/db/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,33 @@ export const getComments = (
(_prismaError) => Errors.other('get comments')
)

/**
* Gets the latest comments on the site
*/
export const getLatestSiteComments = (
siteId: string,
): ResultAsync<Comment.WithAuthorAndPost[], RouteError> =>
ResultAsync.fromPromise(
prisma.comment.findMany({
distinct: 'id',
include: {
author: true,
post: true
},
where: {
post: {
site: {
is: {
id: siteId,
},
},
},
},
orderBy: { created_at: 'desc' }
}),
(_prismaError) => Errors.other('get latest site comments')
)

export const createComment = (
postId: string,
{ body, parentCommentId, authorId, anonAuthorName }: Comment.Raw
Expand Down
5 changes: 5 additions & 0 deletions src/db/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export namespace Comment {
author: Nullable<User>
}

export type WithAuthorAndPost = Comment & {
post: Post,
author: Nullable<User>
}

// This is the "raw" query response from prisma
// Recursive comment tree
export type WithRepliesAndAuthor = WithAuthor & {
Expand Down
34 changes: 34 additions & 0 deletions src/routes/admins/sites/get-site-comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { decode } from 'routes/parser'
import * as rt from 'runtypes'
import validator from 'validator'
import { err, ResultAsync } from 'neverthrow'

import { protectedRoute, AppData } from 'router'
import * as Errors from 'errors'
import { getLatestSiteComments, getSingleSite } from 'db/actions'
import { User, Comment, Site } from 'db/types'

type RouteError = Errors.RouteError

const siteIdDecoder = rt.String.withConstraint(
(s) => s.startsWith('c') && validator.isAlphanumeric(s)
)

const errorMsg = 'Request path requires a cuid'

const getAdminSiteComments = (
siteId: Site['id'],
adminId: User['id']
): ResultAsync<Comment.WithAuthorAndPost[], RouteError> =>
getSingleSite({ type_: 'Cuid', val: siteId })
.andThen((site) =>
site.owner_id === adminId
? getLatestSiteComments(siteId)
: err(Errors.notFound())
)

export const handler = protectedRoute<Comment.WithAuthorAndPost[]>((req, admin) =>
decode(siteIdDecoder, req.params.id, errorMsg).map((siteId) =>
getAdminSiteComments(siteId, admin.id).map(AppData.init)
)
)
2 changes: 2 additions & 0 deletions src/routes/admins/sites/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Router } from 'express'
import { handler as getAllSites } from './get-all'
import { handler as getSingle } from './get-single'
import { handler as registerSite } from './register-site'
import { handler as getSingleSiteComments } from './get-site-comments'

const router = Router()

router.get('/', getAllSites)
router.post('/register', registerSite)
router.get('/:id', getSingle)
router.get('/:id/comments', getSingleSiteComments)

export default router