mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 11:07:43 +00:00
6bc7737ef0
Fixes #1516
47 lines
981 B
TypeScript
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;
|