Skip to content

Commit

Permalink
Added data query table and modules (#69)
Browse files Browse the repository at this point in the history
* added data_query table

* added DataQuery model objects

* added data query api

* removed cyclic dependencies

* moved dataquery objects to core

* data query delete

* updated arena-core dependency

* solved SonarCloud issue

---------

Co-authored-by: Stefano Ricci <[email protected]>
  • Loading branch information
SteRiccio and SteRiccio authored Apr 12, 2024
1 parent e95fdfc commit 96dc7f4
Show file tree
Hide file tree
Showing 36 changed files with 653 additions and 69 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"dependencies": {
"@godaddy/terminus": "^4.12.1",
"@openforis/arena-core": "^0.0.174",
"@openforis/arena-core": "^0.0.188",
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"compression": "^1.7.4",
Expand Down
7 changes: 5 additions & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Express } from 'express'

import { ExpressInitializer } from '../server'
import { ChainApi } from './chain'

import { AuthApi } from './auth'
import { ChainApi } from './chain'
import { DataQueryApi } from './dataQuery'

export const Api: ExpressInitializer = {
init: (express: Express): void => {
ChainApi.init(express)
AuthApi.init(express)
ChainApi.init(express)
DataQueryApi.init(express)
},
}
32 changes: 32 additions & 0 deletions src/api/dataQuery/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Express } from 'express'

import { ServiceRegistry } from '@openforis/arena-core'

import { ExpressInitializer } from '../../server'
import { ServerServiceType } from '../../server/arenaServer/serverServiceType'
import { DataQueryService } from '../../service'
import { Requests } from '../../utils'

import { ApiEndpoint } from '../endpoint'

const getDataQueryService = (): DataQueryService =>
ServiceRegistry.getInstance().getService(ServerServiceType.dataQuery)

export const DataQueryCreate: ExpressInitializer = {
init: (express: Express): void => {
express.post(ApiEndpoint.dataQuery.dataQuery(':surveyId', ':queryUuid'), async (req, res, next) => {
try {
const { surveyId } = Requests.getParams(req)
const querySummary = req.body

const service = getDataQueryService()

const querySummaryInserted = await service.insert({ surveyId, item: querySummary })

res.json(querySummaryInserted)
} catch (error) {
next(error)
}
})
},
}
31 changes: 31 additions & 0 deletions src/api/dataQuery/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Express } from 'express'

import { ServiceRegistry } from '@openforis/arena-core'

import { ExpressInitializer } from '../../server'
import { ServerServiceType } from '../../server/arenaServer/serverServiceType'
import { DataQueryService } from '../../service'
import { Requests } from '../../utils'

import { ApiEndpoint } from '../endpoint'

const getDataQueryService = (): DataQueryService =>
ServiceRegistry.getInstance().getService(ServerServiceType.dataQuery)

export const DataQueryDelete: ExpressInitializer = {
init: (express: Express): void => {
express.delete(ApiEndpoint.dataQuery.dataQuery(':surveyId', ':queryUuid'), async (req, res, next) => {
try {
const { surveyId, queryUuid } = Requests.getParams(req)

const service = getDataQueryService()

const querySummaryUpdated = await service.deleteItem({ surveyId, uuid: queryUuid })

res.json(querySummaryUpdated)
} catch (error) {
next(error)
}
})
},
}
16 changes: 16 additions & 0 deletions src/api/dataQuery/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Express } from 'express'

import { ExpressInitializer } from '../../server'
import { DataQueryRead } from './read'
import { DataQueryCreate } from './create'
import { DataQueryUpdate } from './update'
import { DataQueryDelete } from './delete'

export const DataQueryApi: ExpressInitializer = {
init: (express: Express): void => {
DataQueryCreate.init(express)
DataQueryRead.init(express)
DataQueryUpdate.init(express)
DataQueryDelete.init(express)
},
}
59 changes: 59 additions & 0 deletions src/api/dataQuery/read.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Express } from 'express'

import { ServiceRegistry } from '@openforis/arena-core'

import { ExpressInitializer } from '../../server'
import { ServerServiceType } from '../../server/arenaServer/serverServiceType'
import { DataQueryService } from '../../service'
import { Requests } from '../../utils'

import { ApiEndpoint } from '../endpoint'

const getDataQueryService = (): DataQueryService =>
ServiceRegistry.getInstance().getService(ServerServiceType.dataQuery) as DataQueryService

export const DataQueryRead: ExpressInitializer = {
init: (express: Express): void => {
express.get(ApiEndpoint.dataQuery.dataQueriesCount(':surveyId'), async (req, res, next) => {
try {
const { surveyId } = Requests.getParams(req)

const service = getDataQueryService()

const count = await service.count({ surveyId })

res.json({ count })
} catch (error) {
next(error)
}
})

express.get(ApiEndpoint.dataQuery.dataQueries(':surveyId'), async (req, res, next) => {
try {
const { surveyId } = Requests.getParams(req)

const service = getDataQueryService()

const list = await service.getAll({ surveyId })

res.json({ list })
} catch (error) {
next(error)
}
})

express.get(ApiEndpoint.dataQuery.dataQuery(':surveyId', ':querySummaryUuid'), async (req, res, next) => {
try {
const { surveyId, querySummaryUuid } = Requests.getParams(req)

const service = getDataQueryService()

const dataQuerySummary = await service.getByUuid({ surveyId, uuid: querySummaryUuid })

res.json(dataQuerySummary)
} catch (error) {
next(error)
}
})
},
}
32 changes: 32 additions & 0 deletions src/api/dataQuery/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Express } from 'express'

import { ServiceRegistry } from '@openforis/arena-core'

import { ExpressInitializer } from '../../server'
import { ServerServiceType } from '../../server/arenaServer/serverServiceType'
import { DataQueryService } from '../../service'
import { Requests } from '../../utils'

import { ApiEndpoint } from '../endpoint'

const getDataQueryService = (): DataQueryService =>
ServiceRegistry.getInstance().getService(ServerServiceType.dataQuery)

export const DataQueryUpdate: ExpressInitializer = {
init: (express: Express): void => {
express.put(ApiEndpoint.dataQuery.dataQuery(':surveyId', ':queryUuid'), async (req, res, next) => {
try {
const { surveyId } = Requests.getParams(req)
const querySummary = req.body

const service = getDataQueryService()

const querySummaryUpdated = await service.update({ surveyId, item: querySummary })

res.json(querySummaryUpdated)
} catch (error) {
next(error)
}
})
},
}
10 changes: 10 additions & 0 deletions src/api/endpoint/dataQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getApiPathSurvey } from './common'

const moduleName = 'data_queries'

export const dataQuery = {
dataQueriesCount: (surveyId: string): string => getApiPathSurvey(surveyId, moduleName, 'count'),
dataQueries: (surveyId: string): string => getApiPathSurvey(surveyId, moduleName),
dataQuery: (surveyId: string, querySummaryUuid: string): string =>
getApiPathSurvey(surveyId, moduleName, querySummaryUuid),
}
2 changes: 2 additions & 0 deletions src/api/endpoint/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { auth } from './auth'
import { chain } from './chain'
import { dataQuery } from './dataQuery'

export const ApiEndpoint = {
auth,
chain,
dataQuery,
}
77 changes: 34 additions & 43 deletions src/api/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Users,
UserService,
} from '@openforis/arena-core'
import { UnauthorizedError } from '../../server'
import { UnauthorizedError } from '../../server/error'
import { Requests } from '../../utils'

type PermissionFn = (user: User, ...args: Array<any>) => boolean
Expand All @@ -34,56 +34,47 @@ const checkPermission = (req: Request, next: NextFunction, permissionFn: Permiss
}
}

const requireSurveyPermission = (permissionFn: PermissionFn) => async (
req: Request,
_res: Response,
next: NextFunction
) => {
try {
const { surveyId } = Requests.getParams(req)
const service = ServiceRegistry.getInstance().getService(ServiceType.survey) as SurveyService
const survey = await service.get({ surveyId })
const requireSurveyPermission =
(permissionFn: PermissionFn) => async (req: Request, _res: Response, next: NextFunction) => {
try {
const { surveyId } = Requests.getParams(req)
const service = ServiceRegistry.getInstance().getService(ServiceType.survey) as SurveyService
const survey = await service.get({ surveyId })

checkPermission(req, next, permissionFn, survey)
} catch (error) {
next(error)
checkPermission(req, next, permissionFn, survey)
} catch (error) {
next(error)
}
}
}

const requireRecordPermission = (permissionFn: PermissionFn) => async (
req: Request,
_res: Response,
next: NextFunction
) => {
try {
const { surveyId, recordUuid } = Requests.getParams(req)
const service = ServiceRegistry.getInstance().getService(ServiceType.record) as RecordService
const record = await service.get({ surveyId, recordUuid })
const requireRecordPermission =
(permissionFn: PermissionFn) => async (req: Request, _res: Response, next: NextFunction) => {
try {
const { surveyId, recordUuid } = Requests.getParams(req)
const service = ServiceRegistry.getInstance().getService(ServiceType.record) as RecordService
const record = await service.get({ surveyId, recordUuid })

checkPermission(req, next, permissionFn, record)
} catch (error) {
next(error)
checkPermission(req, next, permissionFn, record)
} catch (error) {
next(error)
}
}
}

const requireUserPermission = (permissionFn: PermissionFn) => async (
req: Request,
_res: Response,
next: NextFunction
) => {
try {
const { surveyId, userUuid } = Requests.getParams(req)
const serviceRegistry = ServiceRegistry.getInstance()
const surveyService = serviceRegistry.getService(ServiceType.survey) as SurveyService
const userService = serviceRegistry.getService(ServiceType.user) as UserService
const survey = await surveyService.get({ surveyId })
const userToEdit = await userService.get({ userUuid })
const requireUserPermission =
(permissionFn: PermissionFn) => async (req: Request, _res: Response, next: NextFunction) => {
try {
const { surveyId, userUuid } = Requests.getParams(req)
const serviceRegistry = ServiceRegistry.getInstance()
const surveyService = serviceRegistry.getService(ServiceType.survey) as SurveyService
const userService = serviceRegistry.getService(ServiceType.user) as UserService
const survey = await surveyService.get({ surveyId })
const userToEdit = await userService.get({ userUuid })

checkPermission(req, next, permissionFn, survey, userToEdit)
} catch (error) {
next(error)
checkPermission(req, next, permissionFn, survey, userToEdit)
} catch (error) {
next(error)
}
}
}

export const ApiAuthMiddleware = {
// Admin
Expand Down
5 changes: 3 additions & 2 deletions src/db/dbMigrator/dbMigrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ const migrateSchema = async (params: { schema?: string; migrationsFolder?: strin
}

const migrateSurveySchema = async (surveyId: number): Promise<void> => {
logger.info(`starting db migrations for survey ${surveyId}`)
logger.info(`migrations for survey ${surveyId} - start`)
await migrateSchema({ schema: Schemata.getSchemaSurvey(surveyId) })
logger.info(`migrations for survey ${surveyId} - end`)
}

const migrateSurveySchemas = async (): Promise<void> => {
Expand All @@ -33,7 +34,7 @@ const migrateSurveySchemas = async (): Promise<void> => {

logger.info(`starting survey migrations for ${surveyIds.length} surveys`)

for (const surveyId of surveyIds) {
for await (const surveyId of surveyIds) {
await migrateSurveySchema(surveyId)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict'

var dbm
var type
var seed
var fs = require('fs')
var path = require('path')
var Promise

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate
type = dbm.dataType
seed = seedLink
Promise = options.Promise
}

exports.up = function (db) {
var filePath = path.join(__dirname, 'sqls', '20240403130324-add-table-data-query-up.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
console.log('received data: ' + data)

resolve(data)
})
}).then(function (data) {
return db.runSql(data)
})
}

exports.down = function (db) {
var filePath = path.join(__dirname, 'sqls', '20240403130324-add-table-data-query-down.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
console.log('received data: ' + data)

resolve(data)
})
}).then(function (data) {
return db.runSql(data)
})
}

exports._meta = {
version: 1,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Replace with your SQL commands */
Loading

0 comments on commit 96dc7f4

Please sign in to comment.