Skip to content

Commit 65e1fa6

Browse files
authored
Merge pull request #91 from RyanCNP/feature/excel
Feature/excel
2 parents 1387d1c + c6db495 commit 65e1fa6

4 files changed

Lines changed: 99 additions & 14 deletions

File tree

src/interfaces/denuncia.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { StatusDenuncia } from "../enums/statusDenuncia.enum";
2+
import { IUser } from "./usuario";
23

34
export interface IDenuncia {
45
id: number,
@@ -43,4 +44,13 @@ export interface ICreateDenuncia {
4344
idUsuario: number,
4445
imagens?: string[],
4546
categorias?: number[]
47+
}
48+
49+
export interface IDenunciaExcel{
50+
id: IDenuncia['id'],
51+
titulo : IDenuncia['titulo'],
52+
dataPublicacao : IDenuncia['dataPublicacao'],
53+
status: IDenuncia['status'],
54+
pontuacao: IDenuncia['pontuacao']
55+
author: IUser['nome']
4656
}

src/models/denuncia.model.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { DataTypes, Model, Optional } from "sequelize";
1+
import { DataTypes, FindOptions, Model, Optional } from "sequelize";
22
import { IDenuncia } from "../interfaces/denuncia";
3+
import { IUser } from "../interfaces/usuario";
34
import { StatusDenuncia } from "../enums/statusDenuncia.enum";
45
import sequelize from "../config/database.config";
6+
import { UserModel } from ".";
57

68
type DenunciaCreationalAttributes = Optional<IDenuncia, "id">
79

src/models/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import { ComentarioModel } from "./comentario.model";
1313
import { UserModel } from "./user.model";
1414
import { GrupoCategoriaModel } from "./grupo-categoria.model";
1515

16+
DenunciaModel.belongsTo(UserModel, {
17+
foreignKey: 'id_usuario',
18+
as: 'usuario'
19+
})
20+
21+
UserModel.hasMany(DenunciaModel, {
22+
foreignKey: 'id_usuario',
23+
as: 'denuncias'
24+
})
25+
1626
//CATEGORIAS E DENUNCIAS
1727
DenunciaModel.belongsToMany(CategoriaModel, {
1828
through: CategoriaDenunciaModel,

src/services/denuncia.service.ts

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { ICreateDenuncia, IFilterListDenuncia, IDenuncia } from "../interfaces/denuncia"
1+
import type { ICreateDenuncia, IFilterListDenuncia, IDenuncia, IDenunciaExcel } from "../interfaces/denuncia"
22
import { Op } from "sequelize"
3-
import { CategoriaModel, ImagemDenunciaModel, DenunciaModel, GrupoCategoriaModel } from "../models"
3+
import { CategoriaModel, ImagemDenunciaModel, DenunciaModel, GrupoCategoriaModel, UserModel } from "../models"
44
import { ApiError } from "../errors/ApiError.error"
55
import { HttpCode } from "../enums/HttpCode.enum"
66
import ExcelJS from "exceljs";
@@ -190,30 +190,93 @@ function calculatePontuacao(bodyRequest: ICreateDenuncia): number {
190190
}
191191
export const exportDenunciasExcel = async (): Promise<Buffer> => {
192192
const denuncias = await DenunciaModel.findAll({
193-
include: denunciaFindIncludes
194-
});
193+
include: {
194+
model: UserModel,
195+
as: 'usuario',
196+
attributes: ['nome']
197+
},
198+
attributes:
199+
['id', 'titulo', 'dataPublicacao', 'status', 'pontuacao']
200+
}) as unknown as any[]
201+
202+
const mapToExcel :IDenunciaExcel[] = denuncias.map(d => {
203+
return {
204+
dataPublicacao : d.dataPublicacao,
205+
id: d.id,
206+
pontuacao : d.pontuacao,
207+
status : d.status,
208+
titulo: d.titulo,
209+
author: d.usuario?.nome
210+
}
211+
})
195212

196213
const workbook = new ExcelJS.Workbook();
197214
const sheet = workbook.addWorksheet("Denúncias");
215+
console.log(mapToExcel)
198216

199217
sheet.columns = [
200-
{ header: "ID", key: "id", width: 10 },
201-
{ header: "Título", key: "titulo", width: 30 },
202-
{ header: "Descrição", key: "descricao", width: 40 },
203-
{ header: "Status", key: "status", width: 15 },
204-
{ header: "Pontuação", key: "pontuacao", width: 12 },
205-
{ header: "Data", key: "dataPublicacao", width: 20 },
218+
{ header: "ID", key: "id", width: 6 },
219+
{ header: "Título", key: "titulo", width: 50 },
220+
{ header: "Autor", key: "autor", width: 50 },
221+
{ header: "Status", key: "status", width: 12 },
222+
{ header: "Pontuação", key: "pontuacao", width: 15 },
223+
{ header: "Data", key: "dataPublicacao", width: 15 },
206224
];
207225

208-
denuncias.forEach((d) => {
209-
sheet.addRow({
226+
mapToExcel.forEach((d, idx) => {
227+
const row = sheet.addRow({
210228
id: d.id,
211229
titulo: d.titulo,
212-
descricao: d.descricao,
230+
autor: d.author,
213231
status: d.status,
214232
pontuacao: d.pontuacao,
215233
dataPublicacao : d.dataPublicacao
216234
});
235+
const statusCell = sheet.getCell(`D${idx + 2}`);
236+
switch (d.status) {
237+
case 'aberto':
238+
statusCell.fill = {
239+
type: 'pattern',
240+
pattern: 'solid',
241+
fgColor: { argb: 'FFFF0000' } // vermelho
242+
};
243+
statusCell.font = { color: { argb: 'FFFFFFFF' } };
244+
break;
245+
case 'visualizada':
246+
statusCell.fill = {
247+
type: 'pattern',
248+
pattern: 'solid',
249+
fgColor: { argb: 'FF2196F3' } // azul
250+
};
251+
statusCell.font = { color: { argb: 'FFFFFFFF' } };
252+
break;
253+
case 'analise':
254+
statusCell.fill = {
255+
type: 'pattern',
256+
pattern: 'solid',
257+
fgColor: { argb: 'FFFFC107' } // amarelo
258+
};
259+
statusCell.font = { color: { argb: 'FF000000' } };
260+
break;
261+
case 'agendado':
262+
statusCell.fill = {
263+
type: 'pattern',
264+
pattern: 'solid',
265+
fgColor: { argb: 'FF9C27B0' } // roxo
266+
};
267+
statusCell.font = { color: { argb: 'FFFFFFFF' } };
268+
break;
269+
case 'resolvida':
270+
statusCell.fill = {
271+
type: 'pattern',
272+
pattern: 'solid',
273+
fgColor: { argb: 'FF4CAF50' } // verde
274+
};
275+
statusCell.font = { color: { argb: 'FFFFFFFF' } };
276+
break;
277+
default:
278+
break;
279+
}
217280
});
218281

219282
// Gera o arquivo em memória (Buffer | ArrayBuffer)

0 commit comments

Comments
 (0)