Skip to content

Commit

Permalink
Hotfix/eagle eye events (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Reyero authored Aug 25, 2022
1 parent 858e3a5 commit ff65480
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
4 changes: 3 additions & 1 deletion backend/src/api/eagleEyeContent/eagleEyeContentList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export default async (req, res) => {
const payload = await new EagleEyeContentService(req).findAndCountAll(req.query)

if (req.query.filter && Object.keys(req.query.filter).length > 0) {
track('Eagle Eye Content Filtered', { filter: req.query.filter }, { ...req })
const platforms = req.query.filter.platforms ? req.query.filter.platforms.split(',') : []
const nDays = req.query.filter.nDays
track('Eagle Eye Filter', { filter: req.query.filter, platforms, nDays }, { ...req })
}

await ApiResponseHandler.success(req, res, payload)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/eagleEyeContent/eagleEyeContentSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async (req, res) => {

const payload = await new EagleEyeContentService(req).search(req.body.data)

track('EagleEyeContent Manually Created', { ...req.body.data }, { ...req })
track('EagleEyeSearch', { ...req.body.data }, { ...req })

await ApiResponseHandler.success(req, res, payload)
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,22 @@ describe('eagleEyeContentRepository tests', () => {
expect(found.count).toBe(3)
})

it('Filter by nDays', async () => {
const mockIRepositoryOptions = await SequelizeTestUtils.getTestIRepositoryOptions(db)

await addAll(mockIRepositoryOptions)

const found = await EagleEyeContentRepository.findAndCountAll(
{
filter: {
nDays: 1,
},
},
mockIRepositoryOptions,
)
expect(found.count).toBe(3)
})

it('Filter by status NULL', async () => {
const mockIRepositoryOptions = await SequelizeTestUtils.getTestIRepositoryOptions(db)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ export default class EagleEyeContentRepository {
})
}

if (filter.nDays) {
whereAnd.push({
timestamp: {
[Op.gte]: moment().subtract(filter.nDays, 'days').toDate(),
},
})
}

if (filter.title) {
whereAnd.push(SequelizeFilterUtils.ilikeIncludes('eagleEyeContent', 'title', filter.title))
}
Expand Down
33 changes: 33 additions & 0 deletions backend/src/services/eagleEyeContentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IServiceOptions } from './IServiceOptions'
import EagleEyeContentRepository from '../database/repositories/eagleEyeContentRepository'
import { getConfig } from '../config'
import Error400 from '../errors/Error403'
import track from '../segment/track'

interface EagleEyeSearchPoint {
vectorId: string
Expand Down Expand Up @@ -118,11 +119,43 @@ export default class EagleEyeContentService {
const transaction = await SequelizeRepository.createTransaction(this.options.database)

try {
const recordBeforeUpdate = await EagleEyeContentRepository.findById(id, { ...this.options })
const record = await EagleEyeContentRepository.update(id, data, {
...this.options,
transaction,
})

// If we are updating status we want to track it
if (data.status !== recordBeforeUpdate.status) {
// If we are going from null to status, we are either accepting or rejecting
if (data.status && data.status !== null && data.status !== undefined) {
track(
`EagleEye ${data.status}`,
{
...data,
platform: record.platform,
keywords: record.keywords,
title: record.title,
url: record.url,
},
{ ...this.options },
)
// Here we are bringing back a rejected post to the Inbox
} else if (recordBeforeUpdate.status === 'rejected' && data.status === null) {
track(
`EagleEye post from rejected to Inbox`,
{
...data,
platform: record.platform,
keywords: record.keywords,
title: record.title,
url: record.url,
},
{ ...this.options },
)
}
}

await SequelizeRepository.commitTransaction(transaction)

return record
Expand Down

0 comments on commit ff65480

Please sign in to comment.