Skip to content

Commit 36019f8

Browse files
authored
Non existing member attributes in settings (#476)
1 parent 1ab6fd4 commit 36019f8

8 files changed

Lines changed: 38 additions & 31 deletions

File tree

backend/src/api/member/memberExport.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import moment from 'moment'
21
import Permissions from '../../security/permissions'
32
import identifyTenant from '../../segment/identifyTenant'
43
import track from '../../segment/track'
54
import MemberService from '../../services/memberService'
65
import PermissionChecker from '../../services/user/permissionChecker'
76
import { FeatureFlagRedisKey } from '../../types/common'
87
import { RedisCache } from '../../utils/redis/redisCache'
8+
import { getSecondsTillEndOfMonth } from '../../utils/timing'
99

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

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

32-
const endTime = moment().endOf('month')
33-
const startTime = moment()
34-
35-
const secondsRemainingUntilEndOfMonth = endTime.diff(startTime, 'days') * 86400
32+
const secondsRemainingUntilEndOfMonth = getSecondsTillEndOfMonth()
3633

3734
if (!csvCount) {
3835
await csvCountCache.setValue(req.currentTenant.id, '0', secondsRemainingUntilEndOfMonth)

backend/src/api/premium/enrichment/memberEnrich.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import moment from 'moment'
21
import Permissions from '../../../security/permissions'
32
import identifyTenant from '../../../segment/identifyTenant'
43
import MemberEnrichmentService from '../../../services/premium/enrichment/memberEnrichmentService'
54
import PermissionChecker from '../../../services/user/permissionChecker'
65
import { FeatureFlagRedisKey } from '../../../types/common'
76
import { RedisCache } from '../../../utils/redis/redisCache'
87
import track from '../../../segment/track'
8+
import { createServiceLogger } from '../../../utils/logging'
9+
import { getSecondsTillEndOfMonth } from '../../../utils/timing'
10+
11+
const log = createServiceLogger()
912

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

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

24-
const endTime = moment().endOf('month')
25-
const startTime = moment()
27+
const secondsRemainingUntilEndOfMonth = getSecondsTillEndOfMonth()
2628

27-
const secondsRemainingUntilEndOfMonth = endTime.diff(startTime, 'days') * 86400
29+
log.info(secondsRemainingUntilEndOfMonth, 'Seconds remaining')
2830

2931
if (!memberEnrichmentCount) {
3032
await memberEnrichmentCountCache.setValue(

backend/src/api/premium/enrichment/memberEnrichBulk.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import moment from 'moment'
21
import Error403 from '../../../errors/Error403'
32
import { PLAN_LIMITS } from '../../../feature-flags/ensureFlagUpdated'
43
import Permissions from '../../../security/permissions'
@@ -9,6 +8,7 @@ import { FeatureFlag, FeatureFlagRedisKey } from '../../../types/common'
98
import { createServiceLogger } from '../../../utils/logging'
109
import { RedisCache } from '../../../utils/redis/redisCache'
1110
import track from '../../../segment/track'
11+
import { getSecondsTillEndOfMonth } from '../../../utils/timing'
1212

1313
const log = createServiceLogger()
1414

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

5656
// update enrichment count, we'll also check failed enrichments and deduct these from grand total in bulkEnrichmentWorker
57-
const endTime = moment().endOf('month')
58-
const startTime = moment()
59-
60-
const secondsRemainingUntilEndOfMonth = endTime.diff(startTime, 'days') * 86400
57+
const secondsRemainingUntilEndOfMonth = getSecondsTillEndOfMonth()
6158

6259
if (!memberEnrichmentCount) {
6360
await memberEnrichmentCountCache.setValue(

backend/src/feature-flags/setTenantProperties.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import moment from 'moment'
21
import { PostHog } from 'posthog-node'
32
import { API_CONFIG, POSTHOG_CONFIG } from '../config'
43
import AutomationRepository from '../database/repositories/automationRepository'
54
import { Edition, FeatureFlagRedisKey } from '../types/common'
65
import { RedisClient } from '../utils/redis'
76
import { RedisCache } from '../utils/redis/redisCache'
7+
import { getSecondsTillEndOfMonth } from '../utils/timing'
88

99
export default async function setPosthogTenantProperties(
1010
tenant: any,
@@ -23,10 +23,7 @@ export default async function setPosthogTenantProperties(
2323
let csvExportCount = await csvExportCountCache.getValue(tenant.id)
2424
let memberEnrichmentCount = await memberEnrichmentCountCache.getValue(tenant.id)
2525

26-
const endTime = moment().endOf('month')
27-
const startTime = moment()
28-
29-
const secondsRemainingUntilEndOfMonth = endTime.diff(startTime, 'days') * 86400
26+
const secondsRemainingUntilEndOfMonth = getSecondsTillEndOfMonth()
3027

3128
if (!csvExportCount) {
3229
await csvExportCountCache.setValue(tenant.id, '0', secondsRemainingUntilEndOfMonth)

backend/src/serverless/microservices/nodejs/bulk-enrichment/bulkEnrichmentWorker.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import moment from 'moment-timezone'
21
import { PostHog } from 'posthog-node'
32
import { POSTHOG_CONFIG } from '../../../../config'
43
import getUserContext from '../../../../database/utils/getUserContext'
@@ -7,6 +6,7 @@ import MemberEnrichmentService from '../../../../services/premium/enrichment/mem
76
import { FeatureFlagRedisKey } from '../../../../types/common'
87
import { createRedisClient } from '../../../../utils/redis'
98
import { RedisCache } from '../../../../utils/redis/redisCache'
9+
import { getSecondsTillEndOfMonth } from '../../../../utils/timing'
1010

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

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

4644
if (!memberEnrichmentCount) {
4745
await memberEnrichmentCountCache.setValue(

backend/src/services/__tests__/memberService.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,10 +2511,18 @@ describe('MemberService tests', () => {
25112511
[PlatformType.TWITTER]: 'some value',
25122512
},
25132513
}
2514+
const validateAttributes = await memberService.validateAttributes(attributes)
25142515

2515-
await expect(() => memberService.validateAttributes(attributes)).rejects.toThrowError(
2516-
new Error400('en', 'settings.memberAttributes.notFound', 'non-existing-attribute'),
2517-
)
2516+
// member attribute that is non existing in settings, should be omitted after validate
2517+
const expectedValidatedAttributes = {
2518+
[MemberAttributeName.URL]: {
2519+
[PlatformType.GITHUB]: 'https://some-github-url',
2520+
},
2521+
[MemberAttributeName.AVATAR_URL]: {
2522+
[PlatformType.TWITTER]: 'https://some-image-url',
2523+
},
2524+
}
2525+
expect(validateAttributes).toEqual(expectedValidatedAttributes)
25182526
})
25192527

25202528
it('Should throw a 400 Error when the type of an attribute does not match the type in member attribute settings', async () => {

backend/src/services/memberService.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable no-continue */
2+
13
import moment from 'moment-timezone'
24
import lodash from 'lodash'
35
import validator from 'validator'
@@ -80,11 +82,8 @@ export default class MemberService extends LoggingBase {
8082
attributeName,
8183
attributes,
8284
})
83-
throw new Error400(
84-
this.options.language,
85-
'settings.memberAttributes.notFound',
86-
attributeName,
87-
)
85+
delete attributes[attributeName]
86+
continue
8887
}
8988
if (typeof attributes[attributeName] !== 'object') {
9089
attributes[attributeName] = {

backend/src/utils/timing.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
import moment from 'moment'
2+
13
export const timeout = async (delayMilliseconds: number): Promise<void> =>
24
new Promise<void>((resolve) => {
35
setTimeout(resolve, delayMilliseconds)
46
})
7+
8+
export const getSecondsTillEndOfMonth = () => {
9+
const endTime = moment().endOf('month')
10+
const startTime = moment()
11+
12+
return endTime.diff(startTime, 'seconds')
13+
}

0 commit comments

Comments
 (0)