Skip to content

Commit

Permalink
add comment in export bal
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck committed Feb 20, 2024
1 parent fd168c2 commit c26ddb3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Controller, Res, Req, HttpStatus, Get } from '@nestjs/common';
import { Controller, Res, Req, HttpStatus, Get, Query } from '@nestjs/common';
import { Response } from 'express';
import { ApiParam, ApiTags, ApiResponse, ApiOperation } from '@nestjs/swagger';
import {
ApiParam,
ApiTags,
ApiResponse,
ApiOperation,
ApiQuery,
} from '@nestjs/swagger';

import { CustomRequest } from '@/lib/types/request.type';
import { ExportCsvService } from '@/shared/modules/export_csv/export_csv.service';
import { isAdmin } from '@/lib/utils/is-admin.utils';

@ApiTags('export csv')
@Controller('bases-locales')
Expand All @@ -13,10 +20,16 @@ export class ExportCsvController {
@Get(':baseLocaleId/csv')
@ApiOperation({ summary: 'Get Bal csv file', operationId: 'getCsvBal' })
@ApiParam({ name: 'baseLocaleId', required: true, type: String })
@ApiQuery({ name: 'withComment', type: Boolean })
@ApiResponse({ status: HttpStatus.OK })
async getCsvBal(@Req() req: CustomRequest, @Res() res: Response) {
async getCsvBal(
@Req() req: CustomRequest,
@Query('withComment') withComment: string,
@Res() res: Response,
) {
const csvFile: string = await this.exportCsvService.exportToCsv(
req.baseLocale,
withComment === 'true' && isAdmin(req, req.baseLocale),
);
res.status(HttpStatus.OK).attachment('bal.csv').type('csv').send(csvFile);
}
Expand Down
7 changes: 5 additions & 2 deletions libs/shared/src/modules/export_csv/export_csv.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ export class ExportCsvService {
return { voies, toponymes, numeros };
}

async exportToCsv(baseLocale: BaseLocale): Promise<string> {
async exportToCsv(
baseLocale: BaseLocale,
withComment: boolean = false,
): Promise<string> {
const { voies, toponymes, numeros } = await this.getAllFromBal(
baseLocale._id,
);

return exportBalToCsv(voies, toponymes, numeros);
return exportBalToCsv(voies, toponymes, numeros, withComment);
}

async exportVoiesToCsv(baseLocale: BaseLocale): Promise<string> {
Expand Down
12 changes: 10 additions & 2 deletions libs/shared/src/modules/export_csv/utils/export_csv_bal.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type RowType = {
parcelles: string[];
position?: any;
_updated: Date;
comment?: string;
};

type CsvRowType = {
Expand All @@ -48,6 +49,7 @@ type CsvRowType = {
cad_parcelles: string;
source: string;
date_der_maj: string;
commentaire?: string;
};

function formatCleInterop(
Expand Down Expand Up @@ -89,7 +91,7 @@ function extractHeaders(csvRows: CsvRowType[]): string[] {
}

/* eslint camelcase: off */
function createRow(obj: RowType): CsvRowType {
function createRow(obj: RowType, withComment: boolean): CsvRowType {
const row: CsvRowType = {
cle_interop: formatCleInterop(
obj.codeCommune,
Expand All @@ -115,6 +117,10 @@ function createRow(obj: RowType): CsvRowType {
date_der_maj: obj._updated ? obj._updated.toISOString().slice(0, 10) : '',
};

if (withComment) {
row.commentaire = obj.comment;
}

if (obj.nomVoieAlt) {
Object.keys(obj.nomVoieAlt).forEach((o) => {
row['voie_nom_' + o] = obj.nomVoieAlt[o];
Expand Down Expand Up @@ -147,6 +153,7 @@ export async function exportBalToCsv(
voies: Voie[],
toponymes: Toponyme[],
numeros: Numero[],
withComment: boolean,
): Promise<string> {
const voiesIndex: Record<string, Voie> = keyBy(voies, (v) =>
v._id.toHexString(),
Expand Down Expand Up @@ -183,6 +190,7 @@ export async function exportBalToCsv(
nomToponymeAlt: toponyme?.nomAlt || null,
parcelles: n.parcelles,
position: p,
comment: n.comment,
});
});
}
Expand Down Expand Up @@ -215,7 +223,7 @@ export async function exportBalToCsv(
}
});

const csvRows: CsvRowType[] = rows.map((row) => createRow(row));
const csvRows: CsvRowType[] = rows.map((row) => createRow(row, withComment));
const headers: string[] = extractHeaders(csvRows);

return getStream(
Expand Down

0 comments on commit c26ddb3

Please sign in to comment.