Files
streetmerchant/src/notification/redis.ts
T
2020-12-26 07:28:49 -08:00

47 lines
981 B
TypeScript

import {Link, Store} from '../store/model';
import redis, {RedisClient} from 'redis';
import {config} from '../config';
import {logger} from '../logger';
const {url} = config.notifications.redis;
function initRedis(): RedisClient | null {
if (url) {
return redis.createClient({url});
}
return null;
}
function updateRedis(link: Link, store: Store) {
try {
const client = initRedis();
if (client) {
const key = `${store.name}:${link.brand}:${link.model}`
.split(' ')
.join('-');
const value = {
...link,
labels: store.labels,
links: store.links,
name: store.name,
updatedAt: new Date().toUTCString()
};
const redisUpdated = client.set(key, JSON.stringify(value));
if (redisUpdated) {
logger.info('✔ redis updated');
} else {
logger.error(`✖ couldn't update redis for key (${key})`);
}
}
} catch (error: unknown) {
logger.error("✖ couldn't update redis", error);
}
}
export default updateRedis;