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, }; diff --git a/__tests__/unit/models/token.model.test.ts b/__tests__/unit/models/token.model.test.ts index d925bac..1354cde 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/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/config/enums.ts b/src/config/enums.ts new file mode 100644 index 0000000..27f9167 --- /dev/null +++ b/src/config/enums.ts @@ -0,0 +1,32 @@ +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', +} + +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 deleted file mode 100644 index 8832169..0000000 --- a/src/config/tokens.ts +++ /dev/null @@ -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/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/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/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, 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: {