From d018c87835cf0aadc32d647619efc43b46a7175c Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Tue, 14 May 2024 15:16:50 +0400 Subject: [PATCH 1/6] add enums --- src/config/enums.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/config/enums.ts diff --git a/src/config/enums.ts b/src/config/enums.ts new file mode 100644 index 0000000..4ce9382 --- /dev/null +++ b/src/config/enums.ts @@ -0,0 +1,20 @@ +export enum PlatformNames { + Discord = 'discord', + Google = 'google', + GitHub = 'github', + Notion = 'notion', + MediaWiki = 'mediaWiki', + Twitter = 'twitter', +} + +export enum ModuleNames { + Hivemind = 'hivemind', +} + +export enum HivemindPlatformNames { + Discord = 'discord', + Google = 'google', + GitHub = 'github', + Notion = 'notion', + MediaWiki = 'mediaWiki', +} From 2550bed80779419b6dc77af4ee36ee9a4805b0c6 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Tue, 14 May 2024 15:17:07 +0400 Subject: [PATCH 2/6] update tests --- __tests__/unit/models/module.model.test.ts | 6 ++++-- __tests__/unit/models/platform.model.test.ts | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/__tests__/unit/models/module.model.test.ts b/__tests__/unit/models/module.model.test.ts index 6bade90..e056a41 100644 --- a/__tests__/unit/models/module.model.test.ts +++ b/__tests__/unit/models/module.model.test.ts @@ -1,6 +1,8 @@ import { Module, Platform, Community, User } from '../../../src/models'; import { IModule } from '../../../src/interfaces'; import { Types } from 'mongoose'; +import { ModuleNames, PlatformNames } from '../../../src/config/enums'; + // import setupTestDB from '../../utils/setupTestDB'; // setupTestDB(); @@ -10,7 +12,7 @@ describe('Module model', () => { let module: IModule; beforeEach(() => { module = { - name: 'hivemind', + name: ModuleNames.Hivemind, community: new Types.ObjectId(), options: { platforms: [ @@ -19,7 +21,7 @@ describe('Module model', () => { metadata: { selectedChannels: ['c1', 'c2'], }, - name: 'discord', + name: PlatformNames.Discord, }, ], }, diff --git a/__tests__/unit/models/platform.model.test.ts b/__tests__/unit/models/platform.model.test.ts index 8e6c6c2..14f63bf 100644 --- a/__tests__/unit/models/platform.model.test.ts +++ b/__tests__/unit/models/platform.model.test.ts @@ -1,6 +1,7 @@ import { Platform, Community, User, Module } from '../../../src/models'; import { IPlatform } from '../../../src/interfaces'; import { Types } from 'mongoose'; +import { PlatformNames } from '../../../src/config/enums'; // import setupTestDB from '../../utils/setupTestDB'; // setupTestDB(); @@ -10,7 +11,7 @@ describe('Platform model', () => { let platform: IPlatform; beforeEach(() => { platform = { - name: 'google', + name: PlatformNames.Google, community: new Types.ObjectId(), disconnectedAt: null, }; From 2a6a8d28a430566648f4c9c1df910e1a83a1c807 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Tue, 14 May 2024 15:17:13 +0400 Subject: [PATCH 3/6] use enums --- package.json | 2 +- src/index.ts | 1 + src/interfaces/Module.interface.ts | 7 ++++--- src/interfaces/Platfrom.interface.ts | 5 +++-- src/models/schemas/Module.schema.ts | 5 +++-- src/models/schemas/Platform.schema.ts | 3 ++- 6 files changed, 14 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 7e13a13..6ab943c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@togethercrew.dev/db", - "version": "3.0.55", + "version": "3.0.60", "description": "All interactions with DB", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 3801c20..9bfea2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,3 +2,4 @@ export * from './models'; export * from './models/schemas'; export * from './interfaces'; export * from './service'; +export * from './config/enums'; diff --git a/src/interfaces/Module.interface.ts b/src/interfaces/Module.interface.ts index bcf0896..f076ae4 100644 --- a/src/interfaces/Module.interface.ts +++ b/src/interfaces/Module.interface.ts @@ -1,12 +1,13 @@ import { type Model, type Types } from 'mongoose'; +import { type PlatformNames, type ModuleNames } from '../config/enums'; export interface IModule { - name: 'hivemind'; + name: ModuleNames; community: Types.ObjectId; options?: { platforms: Array<{ platform: Types.ObjectId; - name: 'discord' | 'google' | 'github' | 'notion'; + name: PlatformNames; metadata?: Record; // dynamic object since structure can change }>; }; @@ -15,7 +16,7 @@ export interface IModuleUpdateBody { options?: { platforms: Array<{ platform: Types.ObjectId; - name: 'discord' | 'google' | 'github' | 'notion'; + name: PlatformNames; metadata?: Record; // dynamic object since structure can change }>; }; diff --git a/src/interfaces/Platfrom.interface.ts b/src/interfaces/Platfrom.interface.ts index 59e34e0..cded279 100644 --- a/src/interfaces/Platfrom.interface.ts +++ b/src/interfaces/Platfrom.interface.ts @@ -1,7 +1,8 @@ import { type Model, type Types } from 'mongoose'; +import { type PlatformNames } from '../config/enums'; export interface IPlatform { - name: 'google' | 'discord' | 'twitter'; + name: PlatformNames; community: Types.ObjectId; metadata?: Record; // dynamic object since structure can change disconnectedAt?: Date | null; @@ -9,7 +10,7 @@ export interface IPlatform { } export interface IPlatformUpdateBody { - name?: 'google' | 'discord' | 'twitter'; + name?: PlatformNames; community?: Types.ObjectId; metadata?: Record; disconnectedAt?: Date | null; diff --git a/src/models/schemas/Module.schema.ts b/src/models/schemas/Module.schema.ts index 1e8b9b6..7e02c87 100644 --- a/src/models/schemas/Module.schema.ts +++ b/src/models/schemas/Module.schema.ts @@ -1,13 +1,14 @@ import { Schema } from 'mongoose'; import { toJSON, paginate } from './plugins'; import { type IModule, type ModuleModel } from '../../interfaces'; +import { PlatformNames, ModuleNames } from '../../config/enums'; const moduleSchema = new Schema( { name: { type: String, required: true, - enum: ['hivemind'], + enum: Object.values(ModuleNames), }, community: { type: Schema.Types.ObjectId, @@ -28,7 +29,7 @@ const moduleSchema = new Schema( name: { type: String, required: true, - enum: ['discord', 'google', 'github', 'notion'], + enum: Object.values(PlatformNames), }, }, ], diff --git a/src/models/schemas/Platform.schema.ts b/src/models/schemas/Platform.schema.ts index 81e1df0..d87420b 100644 --- a/src/models/schemas/Platform.schema.ts +++ b/src/models/schemas/Platform.schema.ts @@ -2,13 +2,14 @@ import { Schema, type Document, Types } from 'mongoose'; import { toJSON, paginate } from './plugins'; import { type IPlatform, type PlatformModel } from '../../interfaces'; import { Announcement, Community, Platform, User, Module } from '../index'; +import { PlatformNames } from '../../config/enums'; const platformSchema = new Schema( { name: { type: String, required: true, - enum: ['google', 'discord', 'twitter', 'github', 'notion'], + enum: Object.values(PlatformNames), }, metadata: { type: Schema.Types.Mixed, From aa52a835e4b5e2303b6189068c52ceb6dc0cbfb5 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Tue, 14 May 2024 15:22:39 +0400 Subject: [PATCH 4/6] add tokenTypes enum --- __tests__/unit/models/token.model.test.ts | 12 +++++++++--- src/config/enums.ts | 14 +++++++++++++- src/config/tokens.ts | 11 ----------- src/interfaces/Token.interface.ts | 3 ++- src/models/schemas/Token.schema.ts | 4 ++-- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/__tests__/unit/models/token.model.test.ts b/__tests__/unit/models/token.model.test.ts index d925bac..6de9981 100644 --- a/__tests__/unit/models/token.model.test.ts +++ b/__tests__/unit/models/token.model.test.ts @@ -2,6 +2,7 @@ import { Token } from '../../../src/models'; import { IToken } from '../../../src/interfaces'; import { Types } from 'mongoose'; import moment from 'moment'; +import { TokenTypeNames } from '../../../src/config/enums'; describe('Token model', () => { describe('Token validation', () => { @@ -10,7 +11,7 @@ describe('Token model', () => { token = { user: new Types.ObjectId(), token: '4321', - type: 'google_refresh', + type: TokenTypeNames.GOOGLE_REFRESH, expires: moment('2022-02-01 08:30:26.127Z').toDate(), }; }); @@ -20,8 +21,13 @@ describe('Token model', () => { }); test('should throw a validation error if type is invalid', async () => { - token.type = 'invalidToken'; - await expect(new Token(token).validate()).rejects.toThrow(); + const invalidToken = { + user: new Types.ObjectId(), + token: '4321', + type: 'invalidToken', + expires: moment('2022-02-01 08:30:26.127Z').toDate(), + } + await expect(new Token(invalidToken).validate()).rejects.toThrow(); }); }); }); diff --git a/src/config/enums.ts b/src/config/enums.ts index 4ce9382..2ccd692 100644 --- a/src/config/enums.ts +++ b/src/config/enums.ts @@ -11,10 +11,22 @@ export enum ModuleNames { Hivemind = 'hivemind', } -export enum HivemindPlatformNames { +export enum HivemindPlatforNames { Discord = 'discord', Google = 'google', GitHub = 'github', Notion = 'notion', MediaWiki = 'mediaWiki', } + +export enum TokenTypeNames { + ACCESS = 'access', + REFRESH = 'refresh', + DISCORD_ACCESS = 'discord_access', + DISCORD_REFRESH = 'discord_refresh', + TWITTER_ACCESS = 'twitter_access', + TWITTER_REFRESH = 'twitter_refresh', + GOOGLE_ACCESS = 'google_access', + GOOGLE_REFRESH = 'google_refresh', + NOTION_ACCESS = 'notion_access', +}; diff --git a/src/config/tokens.ts b/src/config/tokens.ts index 8832169..e69de29 100644 --- a/src/config/tokens.ts +++ b/src/config/tokens.ts @@ -1,11 +0,0 @@ -export const tokenTypes = { - ACCESS: 'access', - REFRESH: 'refresh', - DISCORD_ACCESS: 'discord_access', - DISCORD_REFRESH: 'discord_refresh', - TWITTER_ACCESS: 'twitter_access', - TWITTER_REFRESH: 'twitter_refresh', - GOOGLE_ACCESS: 'google_access', - GOOGLE_REFRESH: 'google_refresh', - NOTION_ACCESS: 'notion_access', -}; diff --git a/src/interfaces/Token.interface.ts b/src/interfaces/Token.interface.ts index 438aaeb..d235655 100644 --- a/src/interfaces/Token.interface.ts +++ b/src/interfaces/Token.interface.ts @@ -1,9 +1,10 @@ import { type Model, type Types } from 'mongoose'; +import { type TokenTypeNames } from '../config/enums'; export interface IToken { token: string; user: Types.ObjectId; - type: string; + type: TokenTypeNames; expires: Date; blacklisted?: boolean; } diff --git a/src/models/schemas/Token.schema.ts b/src/models/schemas/Token.schema.ts index 7af94ec..fb8b7d6 100644 --- a/src/models/schemas/Token.schema.ts +++ b/src/models/schemas/Token.schema.ts @@ -1,7 +1,7 @@ import { Schema } from 'mongoose'; import { toJSON } from './plugins'; -import { tokenTypes } from '../../config/tokens'; import { type IToken } from '../../interfaces'; +import { TokenTypeNames } from '../../config/enums'; const tokenSchema = new Schema( { @@ -17,7 +17,7 @@ const tokenSchema = new Schema( }, type: { type: String, - enum: Object.values(tokenTypes), + enum: Object.values(TokenTypeNames), required: true, }, expires: { From 374882bd1b2b1568ac9e28926ff9f49fc614f347 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Tue, 14 May 2024 15:23:42 +0400 Subject: [PATCH 5/6] format the code --- __tests__/unit/models/token.model.test.ts | 2 +- src/config/enums.ts | 2 +- src/config/tokens.ts | 0 3 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 src/config/tokens.ts diff --git a/__tests__/unit/models/token.model.test.ts b/__tests__/unit/models/token.model.test.ts index 6de9981..1354cde 100644 --- a/__tests__/unit/models/token.model.test.ts +++ b/__tests__/unit/models/token.model.test.ts @@ -26,7 +26,7 @@ describe('Token model', () => { token: '4321', type: 'invalidToken', expires: moment('2022-02-01 08:30:26.127Z').toDate(), - } + }; await expect(new Token(invalidToken).validate()).rejects.toThrow(); }); }); diff --git a/src/config/enums.ts b/src/config/enums.ts index 2ccd692..4759f6a 100644 --- a/src/config/enums.ts +++ b/src/config/enums.ts @@ -29,4 +29,4 @@ export enum TokenTypeNames { GOOGLE_ACCESS = 'google_access', GOOGLE_REFRESH = 'google_refresh', NOTION_ACCESS = 'notion_access', -}; +} diff --git a/src/config/tokens.ts b/src/config/tokens.ts deleted file mode 100644 index e69de29..0000000 From 72792f083fa6747cf907e5850145320488ca2aa2 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Tue, 14 May 2024 15:37:04 +0400 Subject: [PATCH 6/6] fix typo --- src/config/enums.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/enums.ts b/src/config/enums.ts index 4759f6a..27f9167 100644 --- a/src/config/enums.ts +++ b/src/config/enums.ts @@ -11,7 +11,7 @@ export enum ModuleNames { Hivemind = 'hivemind', } -export enum HivemindPlatforNames { +export enum HivemindPlatformNames { Discord = 'discord', Google = 'google', GitHub = 'github',