Skip to content

Commit

Permalink
fix: name of the returned object & name of the function
Browse files Browse the repository at this point in the history
Signed-off-by: Sai Sankeerth <[email protected]>
  • Loading branch information
Sai Sankeerth committed Nov 9, 2023
1 parent 5bdc2c6 commit a825926
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
18 changes: 11 additions & 7 deletions src/helpers/geoLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ import * as GenericFieldMappingJson from '../v0/util/data/GenericFieldMapping.js

export default class GeoLocationHelper {
public static getAddressKeyAndValue(message: Record<string, FixMe>): {
addressKey: string;
address: Record<string, FixMe>;
key: string;
value: Record<string, FixMe>;
} {
const { value: address, key: foundKey } =
getFirstMatchingKeyAndValue(message, GenericFieldMappingJson.address) || {};
const { value, key: foundKey } = getFirstMatchingKeyAndValue(
message,
GenericFieldMappingJson.address,
);
const { key: traitsKey } = getFirstMatchingKeyAndValue(message, GenericFieldMappingJson.traits);
const addressKey =
foundKey || (traitsKey ? `${traitsKey}.address` : GenericFieldMappingJson.address[0]);
return { addressKey, address };
return { key: addressKey, value };
}

public static getGeoLocationData(message: Record<string, FixMe>): Record<string, FixMe> {
public static getMessageWithGeoLocationData(
message: Record<string, FixMe>,
): Record<string, FixMe> {
const msg = cloneDeep(message || {});
if (isEmpty(msg?.context?.geo || {})) {
// geo-location data was not sent
return {};
}
const { address, addressKey } = GeoLocationHelper.getAddressKeyAndValue(message);
const { value: address, key: addressKey } = GeoLocationHelper.getAddressKeyAndValue(message);
const addressFieldMapping = {
city: 'city',
country: 'country',
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/enricher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class Enricher {
data: RouterTransformationRequestData[],
): RouterTransformationRequestData[] {
return data.map((inpEv) => {
const geoEnrichedMessage = GeoLocationHelper.getGeoLocationData(inpEv.message);
const geoEnrichedMessage = GeoLocationHelper.getMessageWithGeoLocationData(inpEv.message);
return {
...inpEv,
message: {
Expand Down
22 changes: 11 additions & 11 deletions test/helper/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('GeoLocationHelper tests', () => {
userId: 'abcd-124',
};

const enhancedMsg = GeoLocationHelper.getGeoLocationData(msg);
const enhancedMsg = GeoLocationHelper.getMessageWithGeoLocationData(msg);

expect(enhancedMsg.context.traits.address).not.toBe(undefined);
expect(enhancedMsg.context.traits.address).toEqual({
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('GeoLocationHelper tests', () => {
userId: 'abcd-124',
};

const enhancedMsg = GeoLocationHelper.getGeoLocationData(msg);
const enhancedMsg = GeoLocationHelper.getMessageWithGeoLocationData(msg);

expect(enhancedMsg.context.traits.address).not.toBe(undefined);
expect(enhancedMsg.context.traits.address).toEqual({
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('GeoLocationHelper tests', () => {
userId: 'abcd-124',
};

const enhancedMsg = GeoLocationHelper.getGeoLocationData(msg);
const enhancedMsg = GeoLocationHelper.getMessageWithGeoLocationData(msg);

expect(enhancedMsg.traits.address).not.toBe(undefined);
expect(enhancedMsg.traits.address).toEqual({
Expand Down Expand Up @@ -276,7 +276,7 @@ describe('GeoLocationHelper tests', () => {
userId: 'abcd-124',
};

const enhancedMsg = GeoLocationHelper.getGeoLocationData(msg);
const enhancedMsg = GeoLocationHelper.getMessageWithGeoLocationData(msg);

expect(enhancedMsg.traits.address).not.toBe(undefined);
expect(enhancedMsg.traits.address).toEqual({
Expand Down Expand Up @@ -351,7 +351,7 @@ describe('GeoLocationHelper tests', () => {
userId: 'abcd-124',
};

const enhancedMsg = GeoLocationHelper.getGeoLocationData(msg);
const enhancedMsg = GeoLocationHelper.getMessageWithGeoLocationData(msg);

expect(enhancedMsg.traits.address).not.toBe(undefined);
expect(enhancedMsg.traits.address).toEqual({
Expand Down Expand Up @@ -408,15 +408,15 @@ describe('GeoLocationHelper tests', () => {
userId: 'abcd-124',
};

const enhancedMsg = GeoLocationHelper.getGeoLocationData(msg);
const enhancedMsg = GeoLocationHelper.getMessageWithGeoLocationData(msg);

expect(enhancedMsg).toEqual({});
});
});

describe('get addressKey & address tests', () => {
test('addressKey should be "traits.address", when address is not present but traits object is present', () => {
const { addressKey } = GeoLocationHelper.getAddressKeyAndValue({
const { key: addressKey } = GeoLocationHelper.getAddressKeyAndValue({
traits: {
name: 'Bruce Wayne',
age: 35,
Expand All @@ -427,7 +427,7 @@ describe('get addressKey & address tests', () => {
});

test('addressKey should be "context.traits.address", when address is not present but traits object is present', () => {
const { addressKey } = GeoLocationHelper.getAddressKeyAndValue({
const { key: addressKey } = GeoLocationHelper.getAddressKeyAndValue({
context: {
traits: {
name: 'Bruce Wayne',
Expand All @@ -440,7 +440,7 @@ describe('get addressKey & address tests', () => {
});

test('addressKey should be "traits.address", when traits object is not present at all', () => {
const { addressKey } = GeoLocationHelper.getAddressKeyAndValue({
const { key: addressKey } = GeoLocationHelper.getAddressKeyAndValue({
anonymousId: '129893-2idi9292',
originalTimestamp: '2020-04-17T14:42:44.722Z',
receivedAt: '2020-04-17T20:12:44.758+05:30',
Expand All @@ -465,7 +465,7 @@ describe('get addressKey & address tests', () => {
});

test('addressKey should be "context.traits.address", when address is present in context.traits & address is not present in traits', () => {
const { addressKey } = GeoLocationHelper.getAddressKeyAndValue({
const { key: addressKey } = GeoLocationHelper.getAddressKeyAndValue({
anonymousId: '129893-2idi9292',
originalTimestamp: '2020-04-17T14:42:44.722Z',
receivedAt: '2020-04-17T20:12:44.758+05:30',
Expand Down Expand Up @@ -499,7 +499,7 @@ describe('get addressKey & address tests', () => {
});

test('addressKey should be "traits.address", when empty payload is sent', () => {
const { addressKey } = GeoLocationHelper.getAddressKeyAndValue({});
const { key: addressKey } = GeoLocationHelper.getAddressKeyAndValue({});
expect(addressKey).toEqual('traits.address');
});
});

0 comments on commit a825926

Please sign in to comment.