From 454791a522e539f75b590630df14d91c36e42f83 Mon Sep 17 00:00:00 2001 From: anilb Date: Tue, 31 Jan 2023 10:40:48 +0100 Subject: [PATCH 1/2] instead of throwing error, we delete the member attribute from the payload --- .../src/services/__tests__/memberService.test.ts | 14 +++++++++++--- backend/src/services/memberService.ts | 9 ++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/backend/src/services/__tests__/memberService.test.ts b/backend/src/services/__tests__/memberService.test.ts index 0bfbce10f0..5a3fe01c7e 100644 --- a/backend/src/services/__tests__/memberService.test.ts +++ b/backend/src/services/__tests__/memberService.test.ts @@ -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 () => { diff --git a/backend/src/services/memberService.ts b/backend/src/services/memberService.ts index ffaab3e592..47d5baaaee 100644 --- a/backend/src/services/memberService.ts +++ b/backend/src/services/memberService.ts @@ -1,3 +1,5 @@ +/* eslint-disable no-continue */ + import moment from 'moment-timezone' import lodash from 'lodash' import validator from 'validator' @@ -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] = { From 4084514c7d42612f61d9dead33109d5b72a8c705 Mon Sep 17 00:00:00 2001 From: anilb Date: Tue, 31 Jan 2023 11:25:36 +0100 Subject: [PATCH 2/2] seconds till end of month problem fixed and refactored --- backend/src/api/member/memberExport.ts | 7 ++----- backend/src/api/premium/enrichment/memberEnrich.ts | 10 ++++++---- backend/src/api/premium/enrichment/memberEnrichBulk.ts | 7 ++----- backend/src/feature-flags/setTenantProperties.ts | 7 ++----- .../nodejs/bulk-enrichment/bulkEnrichmentWorker.ts | 6 ++---- backend/src/utils/timing.ts | 9 +++++++++ 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/backend/src/api/member/memberExport.ts b/backend/src/api/member/memberExport.ts index 79212d2d57..ea86060824 100644 --- a/backend/src/api/member/memberExport.ts +++ b/backend/src/api/member/memberExport.ts @@ -1,4 +1,3 @@ -import moment from 'moment' import Permissions from '../../security/permissions' import identifyTenant from '../../segment/identifyTenant' import track from '../../segment/track' @@ -6,6 +5,7 @@ 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 @@ -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) diff --git a/backend/src/api/premium/enrichment/memberEnrich.ts b/backend/src/api/premium/enrichment/memberEnrich.ts index 46d50c7a93..1b3cd813ce 100644 --- a/backend/src/api/premium/enrichment/memberEnrich.ts +++ b/backend/src/api/premium/enrichment/memberEnrich.ts @@ -1,4 +1,3 @@ -import moment from 'moment' import Permissions from '../../../security/permissions' import identifyTenant from '../../../segment/identifyTenant' import MemberEnrichmentService from '../../../services/premium/enrichment/memberEnrichmentService' @@ -6,6 +5,10 @@ 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) @@ -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( diff --git a/backend/src/api/premium/enrichment/memberEnrichBulk.ts b/backend/src/api/premium/enrichment/memberEnrichBulk.ts index 89b58970a8..12ef5d5867 100644 --- a/backend/src/api/premium/enrichment/memberEnrichBulk.ts +++ b/backend/src/api/premium/enrichment/memberEnrichBulk.ts @@ -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' @@ -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() @@ -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( diff --git a/backend/src/feature-flags/setTenantProperties.ts b/backend/src/feature-flags/setTenantProperties.ts index d26043df07..129abc07bd 100644 --- a/backend/src/feature-flags/setTenantProperties.ts +++ b/backend/src/feature-flags/setTenantProperties.ts @@ -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, @@ -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) diff --git a/backend/src/serverless/microservices/nodejs/bulk-enrichment/bulkEnrichmentWorker.ts b/backend/src/serverless/microservices/nodejs/bulk-enrichment/bulkEnrichmentWorker.ts index 0797a622d5..7693e58fb7 100644 --- a/backend/src/serverless/microservices/nodejs/bulk-enrichment/bulkEnrichmentWorker.ts +++ b/backend/src/serverless/microservices/nodejs/bulk-enrichment/bulkEnrichmentWorker.ts @@ -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' @@ -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 @@ -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( diff --git a/backend/src/utils/timing.ts b/backend/src/utils/timing.ts index 6f9b2a46da..950c68f5b5 100644 --- a/backend/src/utils/timing.ts +++ b/backend/src/utils/timing.ts @@ -1,4 +1,13 @@ +import moment from 'moment' + export const timeout = async (delayMilliseconds: number): Promise => new Promise((resolve) => { setTimeout(resolve, delayMilliseconds) }) + +export const getSecondsTillEndOfMonth = () => { + const endTime = moment().endOf('month') + const startTime = moment() + + return endTime.diff(startTime, 'seconds') +}