-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
export file excel and add DocumentController
- Loading branch information
Showing
8 changed files
with
176 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import fs from 'fs'; | ||
import Document from "../models/Document.js"; | ||
|
||
import checkValidationObjectId from '../libraries/checkValidationObjectId.js'; | ||
|
||
class DocumentController { | ||
async index(req, res) { | ||
try { | ||
const documents = await Document.find(); | ||
if(!documents) { throw { code: 404, message: "DOCUMENT_DATA_NOT_FOUND", data: null, status: false } } | ||
|
||
return res.status(200).json({ | ||
status: true, | ||
message: "LIST_DOCUMENT", | ||
data: documents | ||
}); | ||
} catch (error) { | ||
return res.status(error.code || 500).json({ | ||
status: false, | ||
message: error.message, | ||
data: null | ||
}); | ||
} | ||
} | ||
|
||
async store(req, res) { | ||
try { | ||
const { name, document_type } = req.body; | ||
if(!name) { return { status: false, code: 428, message: "NAME_IS_REQUIRED" } } | ||
if(!document_type) { return { status: false, code: 428, message: "TYPE_IS_REQUIRED" } } | ||
|
||
const data = new Document({ name: name, document_type: document_type }); | ||
const document = await data.save(); | ||
if(!document) { throw { code: 500, message: "Export File Gagal!", data: null, status: false } } | ||
|
||
return res.status(200).json({ | ||
status: true, | ||
message: "Export File Berhasil!", | ||
data: document, | ||
}) | ||
} catch (error) { | ||
return res.status(error.code || 500).json({ | ||
status: false, | ||
message: error.message, | ||
data: null | ||
}); | ||
} | ||
} | ||
|
||
async destroy(req, res) { | ||
try { | ||
const {id} = req.params | ||
if(!id) { throw { code: 420, message: "ID_REQUIRED", data: null, status: false } } | ||
|
||
const checkObjId = await checkValidationObjectId(id, Document, "DOCUMENT", true) | ||
if(!checkObjId.status) return res.status(checkObjId.code).json({ | ||
status: false, | ||
message: checkObjId.message, | ||
data: null | ||
}); | ||
|
||
const document = await Document.findOneAndDelete({ _id: id }) | ||
if(!document) { throw { code: 500, message: "Gagal menghapus file!", data: null, status: false } } | ||
|
||
const filepath = `exports/${checkObjId.data.name}`; | ||
|
||
fs.stat('foo.txt', function(err, stat) { | ||
if (err == null) { | ||
fs.unlink(filepath, (err) => { | ||
if(err) { | ||
console.log(err) | ||
return res.status(500).json({ | ||
status: false, | ||
message: "Gagal menghapus file pada server!", | ||
data: null | ||
}); | ||
} | ||
}) | ||
} else { | ||
console.log('Some other error: ', err.code); | ||
} | ||
}); | ||
|
||
return res.status(200).json({ | ||
status: true, | ||
message: "Berhasil menghapus file!", | ||
data: document, | ||
}) | ||
} catch (error) { | ||
return res.status(error.code || 500).json({ | ||
status: false, | ||
message: error.message, | ||
data: null | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default new DocumentController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Document from "../models/Document.js"; | ||
import TransactionLine from "../models/TransactionLine.js"; | ||
|
||
import TransactionLineService from '../services/TransactionLineService.js'; | ||
|
||
import { fDate } from '../libraries/formatTime.js'; | ||
|
||
class PDFController { | ||
async export(req, res) { | ||
try { | ||
const { startDate, endDate } = req.query; | ||
|
||
const query = await TransactionLineService.generateQuerySearch(req); | ||
if(!query.status) throw { code: query.code, message: "ERROR_QUERY_SEARCH", data: null, status: false } | ||
|
||
let result = TransactionLine.aggregate(query.aggregate); | ||
|
||
const lines = await result; | ||
|
||
const filename = `${fDate(startDate*1)}-${fDate(endDate*1)}.pdf` | ||
// writeFileSync(`exports/${filename}`, pdf); | ||
|
||
const data = new Document({ name: filename, document_type: 'pdf' }); | ||
const document = await data.save(); | ||
|
||
if(!document) { throw { code: 500, message: "Export File Gagal!", data: null, status: false } } | ||
return res.status(200).json({ | ||
status: true, | ||
message: "SUCCESS_EXPORT", | ||
}); | ||
} catch (error) { | ||
if(!error.code) { error.code = 500 } | ||
return res.status(error.code).json({ | ||
status: false, | ||
message: error.message, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default new PDFController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters