Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Router } from 'express';
- import * as resourceService from './service';
- import { findResource, resourceValidator } from './validator';
- const router = Router();
- router.get('/', async (req, res, next) => {
- try {
- const resources = await resourceService.getAllResources();
- const returning = res.json({
- type: 'success',
- value: resources
- });
- return returning;
- } catch (e) {
- return next(e);
- }
- });
- router.get('/:id', async (req, res, next) => {
- try {
- const resources = await resourceService.getResource(req.params.id, req.query);
- return res.json(resources);
- } catch (e) {
- return next(e);
- }
- });
- router.get('/:idComunicado/destinatarios', async (req, res, next) => {
- try {
- const resources = await resourceService.getResourceByComunicado(req.params.idComunicado);
- return res.json(resources);
- } catch (e) {
- return next(e);
- }
- });
- router.post('/', resourceValidator, async (req, res, next) => {
- try {
- let comunicado = req.body;
- comunicado.CODIGO = null;
- let resource = await resourceService.createResource(comunicado);
- res.set({
- Location: `${req.originalUrl}/${resource.CODIGO}`
- });
- return res.json(resource);
- } catch (e) {
- return next(e);
- }
- });
- router.put('/:id', resourceValidator, findResource, async (req, res, next) => {
- try {
- let resource = await resourceService.updateResource(req.body, req.params.id);
- res.set({
- Location: `${req.originalUrl}/${resource.CODIGO}`
- });
- return res.json(resource);
- } catch (e) {
- return next(e);
- }
- });
- router.delete('/:id', async (req, res, next) => {
- try {
- await resourceService.deleteResource(req.params.id);
- return res.json(req.params.id);
- } catch (e) {
- return next(e);
- }
- });
- export default router;
- .
- .
- .
- .
- .
- import { Model } from 'objection';
- import knex from '../../../sigpharma-DBobj';
- import gruposUsuariosModel from '../../seguranca/gruposUsuarios/model';
- import usuariosModel from '../../seguranca/usuarios/model';
- import comunicadoDestinatarioModel from '../comunicadoDestinatario/model';
- Model.knex(knex);
- class Comunicado extends Model {
- static get idColumn() {
- return 'CODIGO';
- }
- static get tableName() {
- return 'comunicado';
- }
- static get relationMappings() {
- return {
- grupos: {
- relation: Model.HasOneRelation,
- modelClass: gruposUsuariosModel,
- join: {
- from: 'comunicado.GRU_CODIGO',
- to: 'grupos_usuarios.GRU_CODIGO'
- }
- },
- usuarios: {
- relation: Model.HasOneRelation,
- modelClass: usuariosModel,
- join: {
- from: 'comunicado.REMETENTE',
- to: 'usuarios.USU_CODIGO'
- }
- },
- destinatarios: {
- relation: Model.HasOneRelation,
- modelClass: comunicadoDestinatarioModel,
- join: {
- from: 'comunicado.CODIGO',
- to: 'comunicado_destinatario.COMUNICADO_CODIGO'
- }
- }
- };
- }
- }
- export default Comunicado;
- .
- .
- .
- .
- .
- import Resource from './model';
- // import { groupBy } from 'rxjs/operator/groupBy';
- // import { raw } from 'objection';
- // import Boom from 'boom';
- export async function getAllResources() {
- const resources = await Resource.query()
- .alias('com')
- .leftJoinRelation('grupos')
- .leftJoinRelation('usuarios')
- .select(
- 'com.CODIGO',
- 'com.ASSUNTO',
- 'com.MENSAGEM',
- 'com.DATA_CRIACAO',
- 'com.DATA_INI_EXIBICAO',
- 'grupos.GRU_DESCRICAO',
- 'usuarios.USU_NOME',
- 'usuarios.USU_CODIGO',
- 'usuarios.USU_USUARIO'
- )
- .orderBy('com.CODIGO', 'DESC');
- return resources;
- }
- export async function getResource(id) {
- const resource = await Resource.query()
- .alias('com')
- .leftJoinRelation('grupos')
- .leftJoinRelation('usuarios')
- .select(
- 'com.CODIGO',
- 'com.ASSUNTO',
- 'com.MENSAGEM',
- 'com.ENVIA_EMAIL',
- 'com.ENVIA_SIG',
- 'com.MENSAGEM',
- 'com.DATA_CRIACAO',
- 'com.DATA_INI_EXIBICAO',
- 'grupos.GRU_DESCRICAO',
- 'usuarios.USU_NOME',
- 'usuarios.USU_CODIGO',
- 'usuarios.USU_USUARIO'
- )
- .where({
- CODIGO: id
- })
- .first();
- return resource;
- }
- /*
- export async function getResourceByComunicado(id) {
- const resources = await Resource.query()
- .joinRelation('destinatarios')
- .joinRelation('usuarios')
- .select(
- '*'
- )
- .where('comunicado.CODIGO', id)
- .groupBy('comunicado.CODIGO', 'destinatarios.COMUNICADO_CODIGO')
- .orderBy('destinatarios.CODIGO', 'DESC')
- .debug();
- return resources;
- }
- */
- export async function getResourceByComunicado(id) {
- const resources = await Resource.query()
- .alias('com')
- .joinRelation('destinatarios.usuarios')
- .select(
- 'com.CODIGO',
- 'com.MENSAGEM',
- 'destinatarios:usuarios.USU_CODIGO',
- 'destinatarios:usuarios.USU_NOME',
- 'destinatarios:usuarios.USU_USUARIO',
- 'destinatarios.DT_VISUALIZADO'
- )
- .where('com.CODIGO', id);
- return resources;
- }
- export async function createResource(resource) {
- const resourceQuery = await Resource.query().insert({
- ASSUNTO: resource.ASSUNTO,
- MENSAGEM: resource.MENSAGEM,
- REMETENTE: resource.REMETENTE,
- DATA_CRIACAO: new Date(),
- DATA_INI_EXIBICAO: resource.DATA_INI_EXIBICAO,
- GRU_CODIGO: resource.GRU_CODIGO,
- TIPO_ALERTA: resource.TIPO_ALERTA,
- ENVIA_EMAIL: resource.ENVIA_EMAIL,
- ENVIA_SIG: resource.ENVIA_SIG,
- ENVIA_APP: resource.ENVIA_APP,
- VISUALIZACAO_OBRIGATORIA: resource.VISUALIZACAO_OBRIGATORIA,
- CODIGO_INTERACAO: resource.CODIGO_INTERACAO
- });
- return resourceQuery;
- }
- export async function updateResource(resource, id) {
- const resourceUpdated = await Resource.query().updateAndFetchById(id, {
- ASSUNTO: resource.ASSUNTO,
- MENSAGEM: resource.MENSAGEM
- });
- return resourceUpdated;
- }
- export async function deleteResource(id) {
- const resource = await Resource.query().deleteById(id);
- return resource;
- }
- .
- .
- .
- .
- import Joi from 'joi';
- import validate from '../../../utils/validate';
- import * as resourceService from './service';
- const SCHEMA = {
- CODIGO: Joi.number().optional(),
- ASSUNTO: Joi.string(),
- MENSAGEM: Joi.string(),
- REMETENTE: Joi.number(),
- DATA_CRIACAO: Joi.date(),
- DATA_INI_EXIBICAO: Joi.date(),
- GRU_CODIGO: Joi.number(),
- TIPO_ALERTA: Joi.number(),
- ENVIA_EMAIL: Joi.number(),
- ENVIA_SIG: Joi.number(),
- ENVIA_APP: Joi.number(),
- VISUALIZACAO_OBRIGATORIA: Joi.number(),
- CODIGO_INTERACAO: [Joi.number(), null]
- };
- function resourceValidator(req, res, next) {
- return validate(req.body, SCHEMA)
- .then(() => next())
- .catch(err => next(err));
- }
- function findResource(req, res, next) {
- return resourceService
- .getResource(req.params.id, req.query)
- .then(() => next())
- .catch(err => next(err));
- }
- export { findResource, resourceValidator };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement