Advertisement
fauzi0309

Scan Barcode Pajak Express

Mar 19th, 2024
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.66 KB | Source Code | 0 0
  1. Service
  2. const FormData = require('form-data');
  3. async scanBarcode(file: Express.Multer.File, body: { masa: string, tahun: string, iscreditable: string, upload: string }){
  4.     try {    
  5.       const ortaxToken = await this.prisma.configGeneral.findFirst({
  6.           where: {
  7.             name: 'ortax_token',
  8.           },
  9.         });
  10.         const ortaxXToken = await this.prisma.configGeneral.findFirst({
  11.           where: {
  12.             name: 'ortax_x_token',
  13.           },
  14.         });
  15.  
  16.       const formData = new FormData();
  17.       formData.append('file', file.buffer, { filename: file.originalname });
  18.       formData.append('masa', body.masa);
  19.       formData.append('tahun', body.tahun);
  20.       formData.append('iscreditable', body.iscreditable);
  21.       formData.append('upload', body.upload);
  22.  
  23.       const url = `https://restdev.pajakexpress.com:9001/efaktur/pm/barcodefile`;
  24.  
  25.       const headers = {
  26.       'x-token': ortaxXToken.value,
  27.       Authorization: `Bearer ${ortaxToken.value}`,
  28.       ...formData.getHeaders(),
  29.     };
  30.  
  31.     const response = await axios.post(url, formData, { headers });
  32.  
  33.     return response.data;
  34.     } catch (error) {
  35.       throw error;
  36.     }
  37.   }
  38.  
  39.  
  40. Controller
  41. import { FileInterceptor } from '@nestjs/platform-express';
  42.   @Post('/efaktur/pm/barcodefile')
  43.   @UseInterceptors(FileInterceptor('file'))
  44.   async uploadFile(
  45.     @UploadedFile() file: Express.Multer.File,
  46.     @Body() body: { masa: string, tahun: string, iscreditable: string, upload: string }
  47.   ){    
  48.     const data = await this.ortaxService.scanBarcode(file, body);  
  49.     return{
  50.       data,
  51.       statusCode: HttpStatus.OK,
  52.       message: 'success',
  53.     }
  54.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement