All files / src/controllers AdminController.ts

83.17% Statements 84/101
60% Branches 42/70
95% Functions 19/20
83.17% Lines 84/101

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261  2x                         2x     2x   29x 26x 26x 23x     6x       1x 1x 1x         2x 1x           2x     2x                 2x 1x 1x       1x                       1x             2x 2x           1x 1x       1x 1x       1x 1x 1x 1x     1x     1x         1x             1x 1x       1x 1x 1x 1x     1x 1x 1x       1x       1x       1x       1x       1x 1x       1x 1x       1x 1x 1x 1x 1x 1x           2x 1x           1x 1x       2x 2x 2x 2x       1x 1x             3x     3x       3x   1x 1x 1x   1x 1x 1x   1x 1x 1x           3x         3x       1x       1x 1x       1x                                    
import { NextFunction, Request, Response } from 'express'
import {
  assertNever,
  BadRequestError,
  Commun,
  ConfigManager,
  EntityConfig,
  EntityIndex,
  EntityModel,
  PluginController,
  ServerError,
  UnauthorizedError
} from '@commun/core'
import { AuthProvider, UserModel } from '@commun/users'
import { AdminModule } from '../AdminModule'
import { JSONSchema7 } from 'json-schema'
 
export class AdminController extends PluginController {
  async validateAdminPermissions (req: Request, res: Response, next: NextFunction) {
    if (req.auth?.id) {
      const authUser = await Commun.getEntityDao('users').findOneById(req.auth.id)
      if (authUser && (authUser as UserModel).admin) {
        return next()
      }
    }
    next(new UnauthorizedError())
  }
 
  async listEntities (req: Request, res: Response) {
    const entities = Commun.getEntities()
    return {
      items: Object.values(entities).map(entity => entity.config)
    }
  }
 
  async getEntity (req: Request, res: Response) {
    const entity = Commun.getEntity(req.params.entityName)
    return {
      item: entity.config
    }
  }
 
  async createEntity (req: Request, res: Response) {
    Iif (!req.body.entityName) {
      throw new BadRequestError('Entity name must be provided')
    }
    const entityConfig: EntityConfig<EntityModel> = {
      entityName: req.body.entityName,
      collectionName: req.body.collectionName || req.body.entityName,
      schema: {
        required: [],
        properties: {},
      },
    }
 
    if (req.body.addUser) {
      entityConfig.schema.required!.push('user')
      entityConfig.schema.properties!.user = {
        $ref: '#user',
        readOnly: true,
      }
      entityConfig.permissions = {
        get: 'anyone',
        create: 'user',
        update: 'own',
        delete: 'own',
        properties: {
          user: {
            create: 'system',
            update: 'system',
          },
        }
      }
      ;(entityConfig.indexes as EntityIndex<EntityModel & { user: string }>[]) = [{
        keys: {
          user: 1
        }
      }]
    }
 
    await ConfigManager.createEntityConfig(req.body.entityName, entityConfig)
    return {
      item: entityConfig
    }
  }
 
  async updateEntity (req: Request, res: Response) {
    const entityConfig = await ConfigManager.mergeEntityConfig(req.params.entityName, req.body)
    return { item: entityConfig }
  }
 
  async deleteEntity (req: Request, res: Response) {
    await ConfigManager.deleteEntity(req.params.entityName)
    return { ok: true }
  }
 
  async updateEntityProperty (req: Request, res: Response) {
    const originalEntityConfig = await ConfigManager.readEntityConfig(req.params.entityName)
    const propertyKey = req.params.propertyKey
    const required = req.body.required
    delete req.body.required
 
    // Update requires
    Iif (required === true && !originalEntityConfig.schema.required?.includes(propertyKey)) {
      originalEntityConfig.schema.required = originalEntityConfig.schema.required || []
      originalEntityConfig.schema.required.push(propertyKey)
    } else Iif (required === false && originalEntityConfig.schema.required?.includes(propertyKey)) {
      const index = originalEntityConfig.schema.required.indexOf(propertyKey)
      originalEntityConfig.schema.required.splice(index, 1)
    }
 
    const schema: JSONSchema7 = {
      ...originalEntityConfig.schema,
      properties: {
        ...(originalEntityConfig.schema.properties || {}),
        [propertyKey]: req.body,
      },
    }
    const entityConfig = await ConfigManager.mergeEntityConfig(req.params.entityName, { schema })
    return { item: entityConfig }
  }
 
  async deleteEntityProperty (req: Request, res: Response) {
    const originalEntityConfig = await ConfigManager.readEntityConfig<EntityModel>(req.params.entityName)
    const propertyKey = req.params.propertyKey
    const properties = originalEntityConfig.schema.properties || {}
    delete properties[propertyKey as keyof EntityModel]
 
    // delete property from required array
    Eif (originalEntityConfig.schema.required?.includes(propertyKey)) {
      const index = originalEntityConfig.schema.required.indexOf(propertyKey)
      originalEntityConfig.schema.required.splice(index, 1)
    }
 
    // delete property from permissions
    Iif (originalEntityConfig.permissions?.properties?.[propertyKey]) {
      delete originalEntityConfig.permissions.properties[propertyKey]
    }
 
    const schema = {
      ...originalEntityConfig.schema,
      properties,
    }
    const entityConfig = await ConfigManager.mergeEntityConfig(req.params.entityName, {
      schema,
      permissions: originalEntityConfig.permissions,
    })
    return { item: entityConfig }
  }
 
  async updateEntityJoinProperties (req: Request, res: Response) {
    const originalEntityConfig = await ConfigManager.readEntityConfig(req.params.entityName)
    const joinProperties = {
      ...originalEntityConfig.joinProperties,
      [req.params.propertyKey]: req.body
    }
    const entityConfig = await ConfigManager.mergeEntityConfig(req.params.entityName, { joinProperties })
    return { item: entityConfig }
  }
 
  async deleteEntityJoinProperty (req: Request, res: Response) {
    const originalEntityConfig = await ConfigManager.readEntityConfig<EntityModel>(req.params.entityName)
    const joinProperties = originalEntityConfig.joinProperties
    Eif (joinProperties) {
      delete joinProperties[req.params.propertyKey]
      const entityConfig = await ConfigManager.mergeEntityConfig(req.params.entityName, { joinProperties })
      return { item: entityConfig }
    }
    return { item: originalEntityConfig }
  }
 
  async getPlugin (req: Request, res: Response) {
    const plugin = Commun.getPlugin(req.params.pluginName)
    return {
      item: plugin.config
    }
  }
 
  async updatePlugin (req: Request, res: Response) {
    const pluginConfig = await ConfigManager.mergePluginConfig(req.params.pluginName, req.body)
    return { item: pluginConfig }
  }
 
  async createOrUpdateEmailTemplate (req: Request, res: Response) {
    const templateName = req.params.templateName || req.body.templateName
    delete req.body.templateName
    await ConfigManager.setPluginFile(req.params.pluginName, `templates/${templateName}.json`, req.body)
    return { ok: true }
  }
 
  async deleteEmailTemplate (req: Request, res: Response) {
    await ConfigManager.deletePluginFile(req.params.pluginName, `templates/${req.params.templateName}.json`)
    return { ok: true }
  }
 
  /**
   * Updates the project's .env file with the variables for the provider
   */
  async updateSocialLoginCredentials (req: Request, res: Response) {
    Iif (!req.body.id || !req.body.secret) {
      throw new BadRequestError('ID and Secret are required')
    }
    const provider = req.params.provider as AuthProvider
    let idVariable: string
    let secretVariable: string
 
    switch (provider) {
      case 'google':
        idVariable = 'GOOGLE_CLIENT_ID'
        secretVariable = 'GOOGLE_CLIENT_SECRET'
        break
      case 'facebook':
        idVariable = 'FACEBOOK_APP_ID'
        secretVariable = 'FACEBOOK_APP_SECRET'
        break
      case 'github':
        idVariable = 'GITHUB_CLIENT_ID'
        secretVariable = 'GITHUB_CLIENT_SECRET'
        break
      default:
        assertNever(provider)
        throw new Error('Unknown provider')
    }
 
    await ConfigManager.setEnvironmentVariable({
      [idVariable]: req.body.id,
      [secretVariable]: req.body.secret,
    })
 
    return { ok: true }
  }
 
  async getCommunSettings (req: Request, res: Response) {
    return await ConfigManager.getCommunOptions()
  }
 
  async setCommunSettings (req: Request, res: Response) {
    await ConfigManager.setCommunOptions(req.params.env, req.body)
    return { ok: true }
  }
 
  async getServerSettings (req: Request, res: Response) {
    return {
      startTime: AdminModule.getServerStartTime(),
      environment: process.env.NODE_ENV,
      communVersion: (process.env.npm_package_dependencies__commun_core || '').replace(/^\^/, ''),
    }
  }
 
  async createAdmin (req: Request, res: Response) {
    AdminModule.validateFirstRunCode(req.body.code)
    const usersEntity = Commun.getEntity<UserModel>('users')
    const result = await usersEntity.controller.create(req)
    if (!result.item.id) {
      throw new ServerError('Error occurred when creating the account, please try again')
    }
    await usersEntity.dao.updateOne(result.item.id, { admin: true })
    return result
  }
}