Skip to content

Commit fa6fb7e

Browse files
committed
adding the std bank schema and updating the syntax
of the files todo:jwt for authentication
1 parent efc6251 commit fa6fb7e

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

models/std_bank.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
const { connector } = require("./databaseUtil");
2+
3+
const BankSchema = {
4+
userId: {
5+
type: String,
6+
required: true,
7+
unique: true,
8+
},
9+
bank_name: {
10+
type: String,
11+
required: true,
12+
minLength: 7,
13+
},
14+
bank_acc: {
15+
type: String,
16+
required: true,
17+
unique: true,
18+
},
19+
bank_branch: {
20+
type: String,
21+
required: true,
22+
},
23+
bank_ifsc: {
24+
type: String,
25+
required: true,
26+
maxLength: 11,
27+
minLength: 11,
28+
},
29+
bank_micr: {
30+
type: String,
31+
required: true,
32+
maxLength: 9,
33+
minLength: 9,
34+
},
35+
};
36+
37+
const Bank = new connector.model("Bank", BankSchema);
38+
39+
export const AddBankdetails = async (req, res) => {
40+
const id = req.body.userId;
41+
const newBank = new Bank({
42+
userId: id, //we will get it after by jwt
43+
...req.body,
44+
});
45+
try {
46+
const existing = await Bank.findOne({ userId: id });
47+
if (existing) return res.status(400).send("Bank details already exists");
48+
await newBank.save();
49+
res.status(201).json("Bank account details added");
50+
} catch (error) {
51+
console.log(error);
52+
}
53+
};
54+
export const GetBankdetails = async (req, res) => {
55+
const id = req.params.id; //get it by jwt as user loggs in we will have it;
56+
try {
57+
const data = await Bank.findOne({ userId: id });
58+
if (!data) return res.status(404).send("No data found for this id");
59+
60+
res.status(200).json(data);
61+
} catch (error) {
62+
console.log(error);
63+
}
64+
};
65+
export const updateBankdetails = async (req, res) => {
66+
const id = req.params.id;
67+
try {
68+
const data = await Bank.findOne({ _id: id });
69+
if (!data) return res.status(404).send("bank account details not found");
70+
71+
// if(data.userId !==req.userId ) thsi statement will help us know that if the user is the specific user trying to update
72+
const datas = await Bank.findByIdAndUpdate(
73+
req.params.id,
74+
{ $set: req.body },
75+
{ new: true }
76+
);
77+
res.status(200).json(datas);
78+
} catch (error) {
79+
console.log(error);
80+
}
81+
};
82+
export const deleteBankdetails = async (req, res) => {
83+
const id = req.params.id;
84+
try {
85+
const data = await Bank.findOne({ _id: id });
86+
if (!data) return res.status(404).send("bank account details not found");
87+
// if(data.userId !==req.userId ) this statement will help us know that if the user is the specific user trying to delete
88+
89+
await Bank.findByIdAndDelete(req.params.id);
90+
res.status(200).send("Bank account deleted");
91+
} catch (error) {
92+
console.log(error);
93+
}
94+
};

0 commit comments

Comments
 (0)