Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. import { Router } from 'express';
  2. import * as resourceService from './service';
  3. import { findResource, resourceValidator } from './validator';
  4.  
  5. const router = Router();
  6.  
  7. router.get('/', async (req, res, next) => {
  8. try {
  9. const resources = await resourceService.getAllResources();
  10.  
  11. const returning = res.json({
  12. type: 'success',
  13. value: resources
  14. });
  15.  
  16. return returning;
  17. } catch (e) {
  18. return next(e);
  19. }
  20. });
  21.  
  22. router.get('/:id', async (req, res, next) => {
  23. try {
  24. const resources = await resourceService.getResource(req.params.id, req.query);
  25.  
  26. return res.json(resources);
  27. } catch (e) {
  28. return next(e);
  29. }
  30. });
  31.  
  32. router.get('/:idComunicado/destinatarios', async (req, res, next) => {
  33. try {
  34. const resources = await resourceService.getResourceByComunicado(req.params.idComunicado);
  35.  
  36. return res.json(resources);
  37. } catch (e) {
  38. return next(e);
  39. }
  40. });
  41.  
  42. router.post('/', resourceValidator, async (req, res, next) => {
  43. try {
  44. let comunicado = req.body;
  45. comunicado.CODIGO = null;
  46.  
  47. let resource = await resourceService.createResource(comunicado);
  48.  
  49. res.set({
  50. Location: `${req.originalUrl}/${resource.CODIGO}`
  51. });
  52.  
  53. return res.json(resource);
  54. } catch (e) {
  55. return next(e);
  56. }
  57. });
  58.  
  59. router.put('/:id', resourceValidator, findResource, async (req, res, next) => {
  60. try {
  61. let resource = await resourceService.updateResource(req.body, req.params.id);
  62. res.set({
  63. Location: `${req.originalUrl}/${resource.CODIGO}`
  64. });
  65.  
  66. return res.json(resource);
  67. } catch (e) {
  68. return next(e);
  69. }
  70. });
  71.  
  72. router.delete('/:id', async (req, res, next) => {
  73. try {
  74. await resourceService.deleteResource(req.params.id);
  75.  
  76. return res.json(req.params.id);
  77. } catch (e) {
  78. return next(e);
  79. }
  80. });
  81.  
  82. export default router;
  83. .
  84. .
  85. .
  86. .
  87. .
  88. import { Model } from 'objection';
  89. import knex from '../../../sigpharma-DBobj';
  90. import gruposUsuariosModel from '../../seguranca/gruposUsuarios/model';
  91. import usuariosModel from '../../seguranca/usuarios/model';
  92. import comunicadoDestinatarioModel from '../comunicadoDestinatario/model';
  93.  
  94. Model.knex(knex);
  95.  
  96. class Comunicado extends Model {
  97. static get idColumn() {
  98. return 'CODIGO';
  99. }
  100.  
  101. static get tableName() {
  102. return 'comunicado';
  103. }
  104.  
  105. static get relationMappings() {
  106. return {
  107. grupos: {
  108. relation: Model.HasOneRelation,
  109. modelClass: gruposUsuariosModel,
  110. join: {
  111. from: 'comunicado.GRU_CODIGO',
  112. to: 'grupos_usuarios.GRU_CODIGO'
  113. }
  114. },
  115. usuarios: {
  116. relation: Model.HasOneRelation,
  117. modelClass: usuariosModel,
  118. join: {
  119. from: 'comunicado.REMETENTE',
  120. to: 'usuarios.USU_CODIGO'
  121. }
  122. },
  123. destinatarios: {
  124. relation: Model.HasOneRelation,
  125. modelClass: comunicadoDestinatarioModel,
  126. join: {
  127. from: 'comunicado.CODIGO',
  128. to: 'comunicado_destinatario.COMUNICADO_CODIGO'
  129. }
  130. }
  131. };
  132. }
  133. }
  134.  
  135. export default Comunicado;
  136. .
  137. .
  138. .
  139. .
  140. .
  141. import Resource from './model';
  142. // import { groupBy } from 'rxjs/operator/groupBy';
  143. // import { raw } from 'objection';
  144. // import Boom from 'boom';
  145.  
  146. export async function getAllResources() {
  147. const resources = await Resource.query()
  148. .alias('com')
  149. .leftJoinRelation('grupos')
  150. .leftJoinRelation('usuarios')
  151. .select(
  152. 'com.CODIGO',
  153. 'com.ASSUNTO',
  154. 'com.MENSAGEM',
  155. 'com.DATA_CRIACAO',
  156. 'com.DATA_INI_EXIBICAO',
  157. 'grupos.GRU_DESCRICAO',
  158. 'usuarios.USU_NOME',
  159. 'usuarios.USU_CODIGO',
  160. 'usuarios.USU_USUARIO'
  161. )
  162. .orderBy('com.CODIGO', 'DESC');
  163.  
  164. return resources;
  165. }
  166.  
  167. export async function getResource(id) {
  168. const resource = await Resource.query()
  169. .alias('com')
  170. .leftJoinRelation('grupos')
  171. .leftJoinRelation('usuarios')
  172. .select(
  173. 'com.CODIGO',
  174. 'com.ASSUNTO',
  175. 'com.MENSAGEM',
  176. 'com.ENVIA_EMAIL',
  177. 'com.ENVIA_SIG',
  178. 'com.MENSAGEM',
  179. 'com.DATA_CRIACAO',
  180. 'com.DATA_INI_EXIBICAO',
  181. 'grupos.GRU_DESCRICAO',
  182. 'usuarios.USU_NOME',
  183. 'usuarios.USU_CODIGO',
  184. 'usuarios.USU_USUARIO'
  185. )
  186. .where({
  187. CODIGO: id
  188. })
  189. .first();
  190.  
  191. return resource;
  192. }
  193. /*
  194. export async function getResourceByComunicado(id) {
  195. const resources = await Resource.query()
  196. .joinRelation('destinatarios')
  197. .joinRelation('usuarios')
  198. .select(
  199. '*'
  200. )
  201. .where('comunicado.CODIGO', id)
  202. .groupBy('comunicado.CODIGO', 'destinatarios.COMUNICADO_CODIGO')
  203. .orderBy('destinatarios.CODIGO', 'DESC')
  204. .debug();
  205.  
  206. return resources;
  207. }
  208. */
  209.  
  210. export async function getResourceByComunicado(id) {
  211. const resources = await Resource.query()
  212. .alias('com')
  213. .joinRelation('destinatarios.usuarios')
  214. .select(
  215. 'com.CODIGO',
  216. 'com.MENSAGEM',
  217. 'destinatarios:usuarios.USU_CODIGO',
  218. 'destinatarios:usuarios.USU_NOME',
  219. 'destinatarios:usuarios.USU_USUARIO',
  220. 'destinatarios.DT_VISUALIZADO'
  221. )
  222. .where('com.CODIGO', id);
  223.  
  224. return resources;
  225. }
  226.  
  227. export async function createResource(resource) {
  228. const resourceQuery = await Resource.query().insert({
  229. ASSUNTO: resource.ASSUNTO,
  230. MENSAGEM: resource.MENSAGEM,
  231. REMETENTE: resource.REMETENTE,
  232. DATA_CRIACAO: new Date(),
  233. DATA_INI_EXIBICAO: resource.DATA_INI_EXIBICAO,
  234. GRU_CODIGO: resource.GRU_CODIGO,
  235. TIPO_ALERTA: resource.TIPO_ALERTA,
  236. ENVIA_EMAIL: resource.ENVIA_EMAIL,
  237. ENVIA_SIG: resource.ENVIA_SIG,
  238. ENVIA_APP: resource.ENVIA_APP,
  239. VISUALIZACAO_OBRIGATORIA: resource.VISUALIZACAO_OBRIGATORIA,
  240. CODIGO_INTERACAO: resource.CODIGO_INTERACAO
  241. });
  242.  
  243. return resourceQuery;
  244. }
  245.  
  246. export async function updateResource(resource, id) {
  247. const resourceUpdated = await Resource.query().updateAndFetchById(id, {
  248. ASSUNTO: resource.ASSUNTO,
  249. MENSAGEM: resource.MENSAGEM
  250. });
  251.  
  252. return resourceUpdated;
  253. }
  254.  
  255. export async function deleteResource(id) {
  256. const resource = await Resource.query().deleteById(id);
  257.  
  258. return resource;
  259. }
  260. .
  261. .
  262. .
  263. .
  264. import Joi from 'joi';
  265. import validate from '../../../utils/validate';
  266. import * as resourceService from './service';
  267.  
  268. const SCHEMA = {
  269. CODIGO: Joi.number().optional(),
  270. ASSUNTO: Joi.string(),
  271. MENSAGEM: Joi.string(),
  272. REMETENTE: Joi.number(),
  273. DATA_CRIACAO: Joi.date(),
  274. DATA_INI_EXIBICAO: Joi.date(),
  275. GRU_CODIGO: Joi.number(),
  276. TIPO_ALERTA: Joi.number(),
  277. ENVIA_EMAIL: Joi.number(),
  278. ENVIA_SIG: Joi.number(),
  279. ENVIA_APP: Joi.number(),
  280. VISUALIZACAO_OBRIGATORIA: Joi.number(),
  281. CODIGO_INTERACAO: [Joi.number(), null]
  282. };
  283.  
  284. function resourceValidator(req, res, next) {
  285. return validate(req.body, SCHEMA)
  286. .then(() => next())
  287. .catch(err => next(err));
  288. }
  289.  
  290. function findResource(req, res, next) {
  291. return resourceService
  292. .getResource(req.params.id, req.query)
  293. .then(() => next())
  294. .catch(err => next(err));
  295. }
  296.  
  297. export { findResource, resourceValidator };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement