From c3c2aa55bf5b9e5c2d5ab21c608dc7b5cd738196 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 17 Mar 2021 12:24:40 +0300 Subject: feat: add initial Spreadhseets service --- src/services/index.ts | 2 ++ src/services/spreadsheets.service.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/services/spreadsheets.service.ts (limited to 'src') diff --git a/src/services/index.ts b/src/services/index.ts index cabfff3..dd1591c 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -2,11 +2,13 @@ import { Application } from '@feathersjs/express'; import Products from './products.service'; import Contractors from './contractors.service'; import Waybills from './waybills.service'; +import Spreadsheets from './spreadsheets.service'; export default (app: Application): void => { app.configure(Products); app.configure(Contractors); app.configure(Waybills); + app.configure(Spreadsheets); app.get('/ping', (req, res) => res.send('pong')); }; diff --git a/src/services/spreadsheets.service.ts b/src/services/spreadsheets.service.ts new file mode 100644 index 0000000..437decc --- /dev/null +++ b/src/services/spreadsheets.service.ts @@ -0,0 +1,31 @@ +import { Application } from '@feathersjs/express'; +import { ServiceMethods, Id } from '@feathersjs/feathers'; +import ExcelJS from 'exceljs'; +import { WaybillSchema } from '../models/waybill/waybill.schema'; + + +class Spreadsheets implements Partial> { + app!: Application; + + setup(app: Application) { + this.app = app; + } + + async get(id: Id) { + const waybill: WaybillSchema = await this.app.service('waybills').get(id); + const workbook = new ExcelJS.Workbook(); + const sheet = workbook.addWorksheet('Накладная'); + + waybill.records.forEach(record => { + sheet.addRow([record.productId, record.quantity, record.price]); + }); + + await workbook.xlsx.writeFile('./documents/waybill.xlsx'); + return 'written!'; + } +} + + +export default (app: Application): void => { + app.use('/spreadsheets', new Spreadsheets); +}; -- cgit v1.2.3