Use the correct data format in the pub/sub queue

This commit is contained in:
Hongbo Wu 2022-10-27 14:01:32 +08:00
parent 02669a2a52
commit 3446bc3555
2 changed files with 14 additions and 5 deletions

View file

@ -13,8 +13,10 @@ import { DateFilter } from '../../utils/search'
export interface Message {
type?: EntityType
data?: any
id?: string
userId: string
pageId?: string
articleId?: string
}
const logger = buildLogger('app.dispatch')
@ -41,7 +43,9 @@ export function integrationsServiceRouter() {
}
try {
const { userId, type, data }: Message = JSON.parse(msgStr)
const data: Message = JSON.parse(msgStr)
const userId = data.userId
const type = data.type
if (!userId) {
logger.info('No userId found in message')
res.status(400).send('Bad Request')
@ -62,7 +66,7 @@ export function integrationsServiceRouter() {
const action = req.params.action.toUpperCase()
if (action === 'SYNC_UPDATED') {
// get updated page by id
let id = ''
let id: string | undefined
switch (type) {
case EntityType.PAGE:
id = data.id
@ -74,6 +78,11 @@ export function integrationsServiceRouter() {
id = data.pageId
break
}
if (!id) {
logger.info('No id found in message')
res.status(400).send('Bad Request')
return
}
const page = await getPageById(id)
if (!page) {
logger.info('No page found for id', { id })

View file

@ -190,7 +190,7 @@ describe('Integrations routers', () => {
JSON.stringify({
userId: user.id,
type: 'page',
data: { id: page.id },
id: page.id,
})
).toString('base64'),
publishTime: new Date().toISOString(),
@ -257,7 +257,7 @@ describe('Integrations routers', () => {
JSON.stringify({
userId: user.id,
type: 'highlight',
data: { articleId: page.id },
articleId: page.id,
})
).toString('base64'),
publishTime: new Date().toISOString(),