Skip to content

Commit

Permalink
Merge branch 'postgres' of https://github.com/ezpaarse-project/ezmesure
Browse files Browse the repository at this point in the history
… into postgres
  • Loading branch information
nojhamster committed Oct 26, 2023
2 parents 0ffb2a9 + baf3318 commit 83d0251
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 13 deletions.
37 changes: 37 additions & 0 deletions api/lib/controllers/harvests/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const harvestsService = require('../../entities/harvest.service');

/**
* @typedef {import('@prisma/client').Prisma.HarvestFindManyArgs} HarvestFindManyArgs
*/

exports.getHarvests = async (ctx) => {
const {
from,
to,
reportId,
size,
sort,
order = 'asc',
page = 1,
} = ctx.request.query;

/** @type {HarvestFindManyArgs} */
const options = {
take: Number.isInteger(size) ? size : undefined,
skip: Number.isInteger(size) ? size * (page - 1) : undefined,
orderBy: sort ? { [sort]: order } : undefined,
where: {
reportId,
},
};

if (from || to) {
options.where.period = { gte: from, lte: to };
}

const harvests = await harvestsService.findMany(options);

ctx.type = 'json';
ctx.status = 200;
ctx.body = harvests;
};
39 changes: 39 additions & 0 deletions api/lib/controllers/harvests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const router = require('koa-joi-router')();
const { Joi } = require('koa-joi-router');

const {
requireJwt,
requireUser,
requireAdmin,
} = require('../../services/auth');

const {
getHarvests,
} = require('./actions');

router.use(
requireJwt,
requireUser,
requireAdmin,
);

router.route({
method: 'GET',
path: '/',
handler: [
getHarvests,
],
validate: {
query: {
from: Joi.string().regex(/^[0-9]{4}-[0-9]{2}$/),
to: Joi.string().regex(/^[0-9]{4}-[0-9]{2}$/),
reportId: Joi.string().trim(),
size: Joi.number().min(0),
page: Joi.number().min(1),
sort: Joi.string(),
order: Joi.string().valid('asc', 'desc'),
},
},
});

module.exports = router;
2 changes: 2 additions & 0 deletions api/lib/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const authorize = require('./auth');
const partners = require('./partners');
const metrics = require('./metrics');
const institutions = require('./institutions');
const harvests = require('./harvests');
const sushi = require('./sushi');
const sushiEndpoints = require('./sushi-endpoints');
const tasks = require('./tasks');
Expand Down Expand Up @@ -73,6 +74,7 @@ app.use(authorize.prefix('/profile').middleware());
app.use(logs.prefix('/logs').middleware());
app.use(files.prefix('/files').middleware());
app.use(institutions.prefix('/institutions').middleware());
app.use(harvests.prefix('/harvests').middleware());
app.use(sushi.prefix('/sushi').middleware());
app.use(sushiEndpoints.prefix('/sushi-endpoints').middleware());
app.use(tasks.prefix('/tasks').middleware());
Expand Down
128 changes: 123 additions & 5 deletions api/lib/controllers/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
{
"name": "Files"
},
{
"name": "Harvests"
},
{
"name": "Indices"
},
Expand Down Expand Up @@ -861,11 +864,21 @@
"description": "A SUSHI exception",
"type": "object",
"properties": {
"Code": { "type": "integer" },
"Severity": { "type": "string" },
"Message": { "type": "string" },
"Data": { "type": "string" },
"Help_URL": { "type": "string" }
"Code": {
"type": "integer"
},
"Severity": {
"type": "string"
},
"Message": {
"type": "string"
},
"Data": {
"type": "string"
},
"Help_URL": {
"type": "string"
}
}
},
"Log": {
Expand Down Expand Up @@ -5806,6 +5819,111 @@
}
}
},
"/harvests": {
"parameters": [],
"get": {
"summary": "Get harvests",
"tags": [
"Harvests"
],
"parameters": [
{
"name": "from",
"description": "Start date of the period",
"in": "query",
"schema": {
"type": "string",
"format": "date",
"pattern": "[0-9]{4}-[0-9]{2}"
}
},
{
"name": "to",
"description": "End date of the period",
"in": "query",
"schema": {
"type": "string",
"format": "date",
"pattern": "[0-9]{4}-[0-9]{2}"
}
},
{
"name": "reportId",
"description": "Filter by report ID",
"in": "query",
"schema": {
"type": "string",
"example": "tr"
}
},
{
"name": "size",
"description": "Number of harvests to get. Defaults to all.",
"in": "query",
"schema": {
"type": "integer"
}
},
{
"name": "page",
"description": "Result page",
"in": "query",
"schema": {
"type": "integer"
}
},
{
"name": "sort",
"description": "A field to sort on",
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "order",
"description": "Sort order",
"in": "query",
"schema": {
"type": "string",
"enum": [
"asc",
"desc"
],
"default": "asc"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Harvest"
}
}
}
}
},
"401": {
"$ref": "#/components/responses/UnauthorizedError"
},
"403": {
"$ref": "#/components/responses/ForbiddenError"
}
},
"operationId": "get-harvests",
"description": "Get harvests",
"security": [
{
"bearerAuth": []
}
]
}
},
"/sushi": {
"get": {
"summary": "Get all Sushi items",
Expand Down
2 changes: 1 addition & 1 deletion api/lib/controllers/sushi/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ exports.getHarvests = async (ctx) => {
page = 1,
} = ctx.request.query;

/** @type HarvestFindManyArgs */
/** @type {HarvestFindManyArgs} */
const options = {
take: Number.isInteger(size) ? size : undefined,
skip: Number.isInteger(size) ? size * (page - 1) : undefined,
Expand Down
3 changes: 0 additions & 3 deletions api/lib/controllers/sushi/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const router = require('koa-joi-router')();
const { Joi } = require('koa-joi-router');

const Institution = require('../../models/Institution');

const {
createSchema,
importSchema,
Expand All @@ -25,7 +23,6 @@ const {
const {
getAll,
getOne,
deleteSushiData,
updateSushi,
addSushi,
harvestSushi,
Expand Down
4 changes: 2 additions & 2 deletions api/lib/hooks/harvest/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const hookEmitter = require('../hookEmitter');
const { createQueue } = require('../utils');

const { appLogger } = require('../../services/logger');
const { SUSHI_CODES } = require('../../services/sushi');

const harvestService = require('../../entities/harvest.service');

Expand Down Expand Up @@ -58,8 +59,7 @@ const onHarvestJobUpdate = queued(async (harvestJob) => {
const data = { ...harvestData, period: periodStr };
if (data.status === 'finished' && !coveredPeriods?.has(periodStr)) {
data.status = 'failed';
// SUSHI_CODES.unavailablePeriod
data.errorCode = 'sushi:3030';
data.errorCode = `sushi:${SUSHI_CODES.unavailablePeriod}`;
}

await harvestService.upsert({
Expand Down
2 changes: 1 addition & 1 deletion front/pages/admin/institutions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
sort-by="name"
item-key="id"
show-select
@current-items="currentItemCount = $event.length"
@pagination="currentItemCount = $event.itemsLength"
>
<template #top>
<v-toolbar flat dense>
Expand Down
2 changes: 1 addition & 1 deletion front/pages/admin/users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
sort-by="username"
item-key="username"
show-select
@current-items="currentItemCount = $event.length"
@pagination="currentItemCount = $event.itemsLength"
>
<template #[`item.isAdmin`]="{ item }">
<v-icon v-if="item.isAdmin">
Expand Down

0 comments on commit 83d0251

Please sign in to comment.