Skip to content

Commit 9fe942c

Browse files
authored
TW-1884: Notification system issues (#199)
1 parent 1b506a3 commit 9fe942c

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { btcExchangeRateProvider, tezExchangeRateProvider } from './utils/coinge
3636
import { CodedError } from './utils/errors';
3737
import { exolixNetworksMap } from './utils/exolix-networks-map';
3838
import { coinGeckoTokens } from './utils/gecko-tokens';
39-
import { getExternalApiErrorPayload, isDefined, isNonEmptyString } from './utils/helpers';
39+
import { getExternalApiErrorPayload, isDefined, isNonEmptyString, isTruthy } from './utils/helpers';
4040
import logger from './utils/logger';
4141
import { getSignedMoonPayUrl } from './utils/moonpay/get-signed-moonpay-url';
4242
import SingleQueryDataProvider from './utils/SingleQueryDataProvider';
@@ -161,7 +161,7 @@ app.post('/api/notifications', basicAuth, async (req, res) => {
161161
: getImageFallback(PlatformType.Extension, type),
162162
mobileImageUrl: isNonEmptyString(mobileImageUrl) ? mobileImageUrl : getImageFallback(PlatformType.Mobile, type),
163163
expirationDate,
164-
isMandatory: isDefined(isMandatory)
164+
isMandatory: isTruthy(isMandatory)
165165
};
166166

167167
await redisClient.lpush('notifications', JSON.stringify(newNotification));

src/notifications/utils/get-notifications.util.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Redis } from 'ioredis';
22

3-
import { isNonEmptyString } from '../../utils/helpers';
3+
import { isNonEmptyString, isTruthy } from '../../utils/helpers';
44
import { Notification, PlatformType } from '../notification.interface';
55

66
export const getNotifications = async (client: Redis, platform: PlatformType, startFromTime: number) => {
@@ -21,10 +21,11 @@ export const getNotifications = async (client: Redis, platform: PlatformType, st
2121
continue;
2222
}
2323

24-
if (
25-
platforms.includes(platform) &&
26-
(isMandatory === true || (createdAtTimestamp > startFromTime && createdAtTimestamp < now))
27-
) {
24+
const platformMatch = platforms.includes(platform);
25+
const mandatoryTimeMatch = isTruthy(isMandatory) && createdAtTimestamp < now;
26+
const timeScopeMatch = createdAtTimestamp > startFromTime && createdAtTimestamp < now;
27+
28+
if (platformMatch && (mandatoryTimeMatch || timeScopeMatch)) {
2829
result.push(notifications[i]);
2930
}
3031
}

src/notifications/utils/get-platforms.util.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { isDefined } from '../../utils/helpers';
1+
import { isTruthy } from '../../utils/helpers';
22
import { PlatformType } from '../notification.interface';
33

44
export const getPlatforms = (mobile?: string, extension?: string) => {
55
let platforms: PlatformType[];
66

7-
if (isDefined(mobile) && isDefined(extension)) {
7+
if (isTruthy(mobile) && isTruthy(extension)) {
88
platforms = [PlatformType.Mobile, PlatformType.Extension];
9-
} else if (isDefined(mobile)) {
9+
} else if (isTruthy(mobile)) {
1010
platforms = [PlatformType.Mobile];
1111
} else {
1212
platforms = [PlatformType.Extension];

src/utils/helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { AxiosError } from 'axios';
22

3+
/** From lodash */
4+
type Truthy<T> = T extends null | undefined | false | '' | 0 | 0n ? never : T;
5+
36
export const range = (start: number, end: number, step = 1) =>
47
Array(Math.ceil((end - start) / step))
58
.fill(0)
@@ -28,6 +31,8 @@ export const emptyFn = () => {};
2831

2932
export const isDefined = <T>(value: T | undefined | null): value is T => value !== undefined && value !== null;
3033

34+
export const isTruthy = <T>(value: T): value is Truthy<T> => Boolean(value);
35+
3136
export const isNonEmptyString = (str: unknown): str is string => typeof str === 'string' && str.length !== 0;
3237

3338
export const getExternalApiErrorPayload = (error: unknown) => {

0 commit comments

Comments
 (0)