Skip to content
Merged
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
7 changes: 2 additions & 5 deletions backend/src/api/member/memberExport.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import moment from 'moment'
import Permissions from '../../security/permissions'
import identifyTenant from '../../segment/identifyTenant'
import track from '../../segment/track'
import MemberService from '../../services/memberService'
import PermissionChecker from '../../services/user/permissionChecker'
import { FeatureFlagRedisKey } from '../../types/common'
import { RedisCache } from '../../utils/redis/redisCache'
import { getSecondsTillEndOfMonth } from '../../utils/timing'

/**
* POST /tenant/{tenantId}/member/export
Expand All @@ -29,10 +29,7 @@ export default async (req, res) => {

const csvCount = await csvCountCache.getValue(req.currentTenant.id)

const endTime = moment().endOf('month')
const startTime = moment()

const secondsRemainingUntilEndOfMonth = endTime.diff(startTime, 'days') * 86400
const secondsRemainingUntilEndOfMonth = getSecondsTillEndOfMonth()

if (!csvCount) {
await csvCountCache.setValue(req.currentTenant.id, '0', secondsRemainingUntilEndOfMonth)
Expand Down
10 changes: 6 additions & 4 deletions backend/src/api/premium/enrichment/memberEnrich.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import moment from 'moment'
import Permissions from '../../../security/permissions'
import identifyTenant from '../../../segment/identifyTenant'
import MemberEnrichmentService from '../../../services/premium/enrichment/memberEnrichmentService'
import PermissionChecker from '../../../services/user/permissionChecker'
import { FeatureFlagRedisKey } from '../../../types/common'
import { RedisCache } from '../../../utils/redis/redisCache'
import track from '../../../segment/track'
import { createServiceLogger } from '../../../utils/logging'
import { getSecondsTillEndOfMonth } from '../../../utils/timing'

const log = createServiceLogger()

export default async (req, res) => {
new PermissionChecker(req).validateHas(Permissions.values.memberEdit)
Expand All @@ -21,10 +24,9 @@ export default async (req, res) => {

const memberEnrichmentCount = await memberEnrichmentCountCache.getValue(req.currentTenant.id)

const endTime = moment().endOf('month')
const startTime = moment()
const secondsRemainingUntilEndOfMonth = getSecondsTillEndOfMonth()

const secondsRemainingUntilEndOfMonth = endTime.diff(startTime, 'days') * 86400
log.info(secondsRemainingUntilEndOfMonth, 'Seconds remaining')

if (!memberEnrichmentCount) {
await memberEnrichmentCountCache.setValue(
Expand Down
7 changes: 2 additions & 5 deletions backend/src/api/premium/enrichment/memberEnrichBulk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import moment from 'moment'
import Error403 from '../../../errors/Error403'
import { PLAN_LIMITS } from '../../../feature-flags/ensureFlagUpdated'
import Permissions from '../../../security/permissions'
Expand All @@ -9,6 +8,7 @@ import { FeatureFlag, FeatureFlagRedisKey } from '../../../types/common'
import { createServiceLogger } from '../../../utils/logging'
import { RedisCache } from '../../../utils/redis/redisCache'
import track from '../../../segment/track'
import { getSecondsTillEndOfMonth } from '../../../utils/timing'

const log = createServiceLogger()

Expand Down Expand Up @@ -54,10 +54,7 @@ export default async (req, res) => {
await sendBulkEnrichMessage(tenant, membersToEnrich)

// update enrichment count, we'll also check failed enrichments and deduct these from grand total in bulkEnrichmentWorker
const endTime = moment().endOf('month')
const startTime = moment()

const secondsRemainingUntilEndOfMonth = endTime.diff(startTime, 'days') * 86400
const secondsRemainingUntilEndOfMonth = getSecondsTillEndOfMonth()

if (!memberEnrichmentCount) {
await memberEnrichmentCountCache.setValue(
Expand Down
7 changes: 2 additions & 5 deletions backend/src/feature-flags/setTenantProperties.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import moment from 'moment'
import { PostHog } from 'posthog-node'
import { API_CONFIG, POSTHOG_CONFIG } from '../config'
import AutomationRepository from '../database/repositories/automationRepository'
import { Edition, FeatureFlagRedisKey } from '../types/common'
import { RedisClient } from '../utils/redis'
import { RedisCache } from '../utils/redis/redisCache'
import { getSecondsTillEndOfMonth } from '../utils/timing'

export default async function setPosthogTenantProperties(
tenant: any,
Expand All @@ -23,10 +23,7 @@ export default async function setPosthogTenantProperties(
let csvExportCount = await csvExportCountCache.getValue(tenant.id)
let memberEnrichmentCount = await memberEnrichmentCountCache.getValue(tenant.id)

const endTime = moment().endOf('month')
const startTime = moment()

const secondsRemainingUntilEndOfMonth = endTime.diff(startTime, 'days') * 86400
const secondsRemainingUntilEndOfMonth = getSecondsTillEndOfMonth()

if (!csvExportCount) {
await csvExportCountCache.setValue(tenant.id, '0', secondsRemainingUntilEndOfMonth)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import moment from 'moment-timezone'
import { PostHog } from 'posthog-node'
import { POSTHOG_CONFIG } from '../../../../config'
import getUserContext from '../../../../database/utils/getUserContext'
Expand All @@ -7,6 +6,7 @@ import MemberEnrichmentService from '../../../../services/premium/enrichment/mem
import { FeatureFlagRedisKey } from '../../../../types/common'
import { createRedisClient } from '../../../../utils/redis'
import { RedisCache } from '../../../../utils/redis/redisCache'
import { getSecondsTillEndOfMonth } from '../../../../utils/timing'

/**
* Sends weekly analytics emails of a given tenant
Expand Down Expand Up @@ -39,9 +39,7 @@ async function bulkEnrichmentWorker(tenantId: string, memberIds: string[]) {
)

// calculate remaining seconds for the end of the month, to set TTL for redis keys
const endTime = moment().endOf('month')
const startTime = moment()
const secondsRemainingUntilEndOfMonth = endTime.diff(startTime, 'days') * 86400
const secondsRemainingUntilEndOfMonth = getSecondsTillEndOfMonth()

if (!memberEnrichmentCount) {
await memberEnrichmentCountCache.setValue(
Expand Down
14 changes: 11 additions & 3 deletions backend/src/services/__tests__/memberService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2511,10 +2511,18 @@ describe('MemberService tests', () => {
[PlatformType.TWITTER]: 'some value',
},
}
const validateAttributes = await memberService.validateAttributes(attributes)

await expect(() => memberService.validateAttributes(attributes)).rejects.toThrowError(
new Error400('en', 'settings.memberAttributes.notFound', 'non-existing-attribute'),
)
// member attribute that is non existing in settings, should be omitted after validate
const expectedValidatedAttributes = {
[MemberAttributeName.URL]: {
[PlatformType.GITHUB]: 'https://some-github-url',
},
[MemberAttributeName.AVATAR_URL]: {
[PlatformType.TWITTER]: 'https://some-image-url',
},
}
expect(validateAttributes).toEqual(expectedValidatedAttributes)
})

it('Should throw a 400 Error when the type of an attribute does not match the type in member attribute settings', async () => {
Expand Down
9 changes: 4 additions & 5 deletions backend/src/services/memberService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-continue */

import moment from 'moment-timezone'
import lodash from 'lodash'
import validator from 'validator'
Expand Down Expand Up @@ -80,11 +82,8 @@ export default class MemberService extends LoggingBase {
attributeName,
attributes,
})
throw new Error400(
this.options.language,
'settings.memberAttributes.notFound',
attributeName,
)
delete attributes[attributeName]
continue
}
if (typeof attributes[attributeName] !== 'object') {
attributes[attributeName] = {
Expand Down
9 changes: 9 additions & 0 deletions backend/src/utils/timing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import moment from 'moment'

export const timeout = async (delayMilliseconds: number): Promise<void> =>
new Promise<void>((resolve) => {
setTimeout(resolve, delayMilliseconds)
})

export const getSecondsTillEndOfMonth = () => {
const endTime = moment().endOf('month')
const startTime = moment()

return endTime.diff(startTime, 'seconds')
}