Skip to content

Commit

Permalink
feat: increase list email to 1000 and search by tag (#2107)
Browse files Browse the repository at this point in the history
* feat: increase list emails max to 1000

* feat: allow query param based on tag

* fix: failing test
  • Loading branch information
zxt-tzx committed Jul 19, 2023
1 parent 7415941 commit 943cf36
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export const InitEmailTransactionalMiddleware = (
async function listMessages(req: Request, res: Response): Promise<void> {
// validation from Joi doesn't carry over into type safety here
// following code transforms query params into type-safe arguments for EmailTransactionalService
const { limit, offset, status, created_at, sort_by } = req.query
const { limit, offset, status, created_at, sort_by, tag } = req.query
const userId: string = req.session?.user?.id.toString() // id is number in session; convert to string for tests to pass (weird)
const filter = created_at ? { createdAt: created_at } : undefined
const sortBy = sort_by?.toString().replace(/[+-]/, '')
Expand All @@ -229,6 +229,7 @@ export const InitEmailTransactionalMiddleware = (
orderBy,
status: status as TransactionalEmailMessageStatus,
filterByTimestamp: filter as TimestampFilter,
tag: tag as string,
})
res.status(200).json({
has_more: hasMore,
Expand Down
3 changes: 2 additions & 1 deletion backend/src/email/routes/email-transactional.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ export const InitEmailTransactionalRoute = (

const listMessagesValidator = {
[Segments.QUERY]: Joi.object({
limit: Joi.number().integer().min(1).max(100).default(10),
limit: Joi.number().integer().min(1).max(1000).default(10),
offset: Joi.number().integer().min(0).default(0),
tag: Joi.string().max(255),
status: Joi.string()
.uppercase()
.valid(...Object.values(TransactionalEmailMessageStatus)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ describe(`GET ${emailTransactionalRoute}`, () => {
.set('Authorization', `Bearer ${apiKey}`)
expect(resInvalidLimit.status).toBe(400)
const resInvalidLimitTooLarge = await request(app)
.get(`${endpoint}?limit=1000`)
.get(`${endpoint}?limit=1001`)
.set('Authorization', `Bearer ${apiKey}`)
expect(resInvalidLimitTooLarge.status).toBe(400)
const resInvalidOffset = await request(app)
Expand Down
9 changes: 7 additions & 2 deletions backend/src/email/services/email-transactional.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ async function listMessages({
orderBy,
status,
filterByTimestamp,
tag,
}: {
userId: string
limit?: number
Expand All @@ -219,17 +220,21 @@ async function listMessages({
orderBy?: Ordering
status?: TransactionalEmailMessageStatus
filterByTimestamp?: TimestampFilter
tag?: string
}): Promise<{ hasMore: boolean; messages: EmailMessageTransactional[] }> {
limit = limit || 10
offset = offset || 0
sortBy = sortBy || TransactionalEmailSortField.Created
orderBy = orderBy || Ordering.DESC
const order: Order = [[sortBy, orderBy]]
const where = ((userId, status, filterByTimestamp) => {
const where = ((userId, status, filterByTimestamp, tag) => {
const where: WhereOptions = { userId } // pre-fill with userId for authentication
if (status) {
where.status = status
}
if (tag) {
where.tag = tag
}
if (filterByTimestamp) {
if (filterByTimestamp.createdAt) {
const { gt, gte, lt, lte } = filterByTimestamp.createdAt
Expand All @@ -248,7 +253,7 @@ async function listMessages({
}
}
return where
})(userId, status, filterByTimestamp)
})(userId, status, filterByTimestamp, tag)
const { count, rows } = await EmailMessageTransactional.findAndCountAll({
limit,
offset,
Expand Down

0 comments on commit 943cf36

Please sign in to comment.