Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkorotkov committed Jun 29, 2024
1 parent 1b52488 commit 548c51f
Show file tree
Hide file tree
Showing 13 changed files with 1,300 additions and 410 deletions.
63 changes: 63 additions & 0 deletions app/bot/keyboards/AbstractPersonalizedCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class AbstractPersonalizedCache {

/** @type {Record<number, Record<string, any>>} */
#cache = {};

/**
* @param {number} userId
* @param {string} entityName
* @param {any} data
*/
setUserCacheEntity(userId, entityName, data) {
if (typeof userId !== 'number')
throw new Error('userId must be number');

if (!this.#cache[userId])
this.#cache[userId] = {};

if (typeof this.#cache[userId][entityName] === 'undefined')
this.#cache[userId][entityName] = structuredClone(data);

return this;
}

/**
* @param {number} userId
* @param {string} [entityName]
*/
getUserCache(userId, entityName) {
if (typeof userId !== 'number')
throw new Error('userId must be number');

const userCache = this.#cache[userId];

if (!userCache)
return undefined;

return entityName !== undefined && entityName ? userCache[entityName] : userCache;
}

/**
* @param {number} userId
* @param {string} [entityName]
*/
flushUserCache(userId, entityName) {
if (typeof userId !== 'number')
throw new Error('userId must be number');

if (!this.#cache[userId])
return this;

if (entityName && this.#cache[userId][entityName]) {
this.#cache[userId][entityName] = undefined;

return this;
}

this.#cache[userId] = undefined;

return this;
}
}

module.exports = AbstractPersonalizedCache;
47 changes: 47 additions & 0 deletions app/bot/keyboards/AbstractPersonalizedCache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { default: expect } = require('expect');
const AbstractPersonalizedCache = require('./AbstractPersonalizedCache');

describe('AbstractPersonalizedCache', () => {

/** @type {AbstractPersonalizedCache} */
let cache;

before(() => {
cache = new AbstractPersonalizedCache();
});

it('should store user cache entity', () => {
expect(() => {
cache.setUserCacheEntity(1, 'key', [1, 2, 3])
}).not.toThrow();
})

it('should return user cache', () => {
const userCache = cache.getUserCache(1);
expect(userCache).toHaveProperty('key', [1, 2, 3]);
});

it('should return user cache entity', () => {
const userCache = cache.getUserCache(1, 'key');
expect(userCache).toEqual([1, 2, 3]);
});

it('should flush user cache entity', () => {
expect(cache.flushUserCache(1, 'key').getUserCache(1, 'key')).toBe(undefined);
expect(cache.flushUserCache(1).getUserCache(1)).toBe(undefined);
});

it('should throw when used incorrectly', () => {
expect(() => {
cache.setUserCacheEntity('key');
}).toThrow();

expect(() => {
cache.getUserCache('key');
}).toThrow();

expect(() => {
cache.flushUserCache('key');
}).toThrow();
});
});
13 changes: 13 additions & 0 deletions app/utils/truncate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const truncate = num => {
if (num >= 1e9) {
return (num / 1e9).toFixed(2) + 'B';
} else if (num >= 1e6) {
return (num / 1e6).toFixed(2) + 'M';
} else if (num >= 1e3) {
return (num / 1e3).toFixed(2) + 'K';
} else {
return num.toFixed(2);
}
}

module.exports = truncate;
6 changes: 0 additions & 6 deletions bot/commands/index.js

This file was deleted.

5 changes: 0 additions & 5 deletions bot/commands/search.js

This file was deleted.

13 changes: 0 additions & 13 deletions bot/commands/start.js

This file was deleted.

5 changes: 0 additions & 5 deletions bot/conversations/index.js

This file was deleted.

62 changes: 0 additions & 62 deletions bot/conversations/search.js

This file was deleted.

56 changes: 0 additions & 56 deletions bot/index.js

This file was deleted.

Loading

0 comments on commit 548c51f

Please sign in to comment.