-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoice.js
89 lines (73 loc) · 2.57 KB
/
invoice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require("dotenv").config();
const router = require('express').Router();
const { MongoClient } = require('mongodb');
const{ObjectId}= require('mongodb');
const cors = require('cors')
var easyinvoice = require('easyinvoice');
const dbURL = process.env.DB_URL || 'mongodb://127.0.0.1:27017'
router.use(cors())
router.post('/createinvoice', async (req, res) => {
try {
let client = await MongoClient.connect(dbURL);
let db = await client.db('organisation');
let data = await db.collection("Invoices-generated").insertOne({ invoice:req.body ,date : new Date()});
if(data){
res.status('200').json({ message: "invoice saved !!" })
} else {
res.status("401").json({ message: "Not saved" })
}
client.close();
} catch (error) {
console.log(error)
res.status(500).json({ message: "Internal server error" })
}
})
router.get('/list', async (req, res) => {
try {
let client = await MongoClient.connect(dbURL);
let db = await client.db('organisation');
let data = await db.collection("Invoices-generated").find().toArray();
if (data) {
res.status(200).json(data);
} else {
res.status(404).json({ message: "data not found" })
}
client.close();
} catch (error) {
console.log(error)
res.status(500).json({ message: "Internal server error" })
}
})
router.post('/list/:id',async(req,res)=>{
try {
let client = await MongoClient.connect(dbURL);
let db = await client.db('organisation');
let data = await db.collection("Invoices-generated").findOne({ _id : ObjectId(req.body.id) });
if(data){
res.status(200).json(data);
} else {
res.status(404).json({ message: "data not found" })
}
client.close();
} catch (error) {
console.log(error)
res.status(500).json({ message: "Internal server error" })
}
})
router.post('/userinvoice',async(req,res)=>{
try {
let client = await MongoClient.connect(dbURL);
let db = await client.db('organisation');
let data = await db.collection("Invoices-generated").find({date:{$gte:new Date(req.body.date)}}).toArray();
if(data){
res.status(200).json(data);
} else {
res.status(404).json({ message: "data not found" })
}
client.close();
} catch (error) {
console.log(error)
res.status(500).json({ message: "Internal server error" })
}
})
module.exports = router;