Skip to content

Commit

Permalink
refactor(notice):move helper functions to utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
gary02 committed Jul 20, 2024
1 parent 66a0030 commit 6d397aa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
10 changes: 7 additions & 3 deletions lib/__test__/notificationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { NotificationType } from '../notification/types'

import { NOTICE_TYPE, OFFICIAL_NOTICE_EXTEND_TYPE } from '../notification/enums'
import { NotificationService } from '../notification'
import { mergeDataWith } from '../notification/utils'
import { getKnexClient } from '../utils/db'

let knex: Knex
Expand All @@ -19,9 +20,12 @@ beforeAll(async () => {
notificationService = new NotificationService({ knex, knexRO: knex })
})

/**
* Notification Service
*/
// utils
test('mergeDataWith', () => {
expect(mergeDataWith({ a: [1, 2] }, { a: [2, 3] })).toEqual({ a: [1, 2, 3] })
})

// service
describe('user notify setting', () => {
const defaultNoifySetting: Record<NotificationType, boolean> = {
// user
Expand Down
13 changes: 2 additions & 11 deletions lib/notification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import type { Language, User } from '../types'

import uniqBy from 'lodash.uniqby'
import { isEqual, mergeWith, uniq } from 'lodash'
import { isEqual } from 'lodash'

import { DAY } from '../constants/index.js'
import { v4 } from 'uuid'
Expand All @@ -28,16 +28,7 @@ import {
} from './enums.js'

import trans, { findTranslation } from './translations.js'
import { loadLatestArticleVersion } from './utils.js'

const mergeDataCustomizer = (objValue: any, srcValue: any) => {
if (Array.isArray(objValue)) {
return uniq(objValue.concat(srcValue))
}
}

const mergeDataWith = (objValue: any, srcValue: any) =>
mergeWith(objValue, srcValue, mergeDataCustomizer)
import { loadLatestArticleVersion, mergeDataWith } from './utils.js'

export class NotificationService {
private knex: Knex
Expand Down
11 changes: 11 additions & 0 deletions lib/notification/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import type { Language } from '../types'
import type { Knex } from 'knex'

import { mergeWith, uniq } from 'lodash'

const mergeDataCustomizer = (objValue: any, srcValue: any) => {
if (Array.isArray(objValue)) {
return uniq(objValue.concat(srcValue))
}
}

export const mergeDataWith = (objValue: any, srcValue: any) =>
mergeWith(objValue, srcValue, mergeDataCustomizer)

export const loadLatestArticleVersion = async (articleId: string, knex: Knex) =>
knex('article_version_newest').where('articleId', articleId).first()

Expand Down

0 comments on commit 6d397aa

Please sign in to comment.