This commit is contained in:
mark H 2024-08-30 16:35:49 +08:00
parent 2bc5041f08
commit c6b13812fe
8 changed files with 228 additions and 190 deletions

View File

@ -12,31 +12,33 @@ const parser = new DatauriParser();
export const register = async (req, res) => { export const register = async (req, res) => {
try { try {
const { const {
fName, // fName,
lName, // lName,
email, email,
password, password,
phone, // phone,
address1, // address1,
address2, // address2,
city, // city,
province, // province,
country, // country,
zip, // zip,
username, // username,
type, // type,
status, // status,
transactions, // transactions,
} = req.body; } = req.body;
cloudinary.config({ cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME, cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY, api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET, api_secret: process.env.CLOUDINARY_API_SECRET,
}); });
// const result = await User.dropIndex('username_1');
// const result = await User.collection.dropIndex('username_1');
const salt = await bcrypt.genSalt(); const salt = await bcrypt.genSalt();
const passwordHash = await bcrypt.hash(password, salt); const passwordHash = await bcrypt.hash(password, salt);
const emailTaken = await User.findOne({ email: email }); const emailTaken = await User.findOne({ email: email });
const usernameTaken = await User.findOne({ username: username }); // const usernameTaken = await User.findOne({ username: username });
// const phoneTaken = await User.findOne({ phone: phone }); // const phoneTaken = await User.findOne({ phone: phone });
const pic = null; const pic = null;
@ -49,24 +51,24 @@ export const register = async (req, res) => {
}); });
} }
if (!emailTaken && !usernameTaken) { if (!emailTaken) {
const newUser = new User({ const newUser = new User({
fName, // fName,
lName, // lName,
email, email,
password: passwordHash, password: passwordHash,
phone, // phone,
address1, // address1,
address2, // address2,
city, // city,
province, // province,
country, // country,
zip, // zip,
username, // username,
type, // type,
status, // status,
transactions, // transactions,
photo: pic ? pic : "", // photo: pic ? pic : "",
}); });
const usersaved = await newUser.save(); const usersaved = await newUser.save();
const user = await User.findOne({ email: email }); const user = await User.findOne({ email: email });
@ -113,7 +115,7 @@ export const login = async (req, res) => {
const user = await User.findOne().or([ const user = await User.findOne().or([
{ email: users }, { email: users },
{ phone: users }, { phone: users },
{ username: users }, // { username: users },
]); ]);
if (!user) { if (!user) {
console.log("does not exist"); console.log("does not exist");

View File

@ -38,6 +38,27 @@ export const updateCollection = async (req, res, next) => {
} }
}; };
export const updateCollectionByEndpoint = async (req, res, next) => {
try {
const { id, endpoint_id } = req.params;
const updatedCollection = await Collection.findByIdAndUpdate(
req.params.id,
{ $push: { endpoints: req.body.endpoint_id } },
{ new: true }
);
if (updatedCollection) {
console.log("Updated Collection:", endpoint_id);
res.status(200).json(updatedCollection);
} else {
res.status(404).json({ endpoint: "Collection or endpoint not found." });
}
} catch (err) {
next(err);
}
};
export const deleteCollection = async (req, res, next) => { export const deleteCollection = async (req, res, next) => {
try { try {
const getCollection = await Collection.findByIdAndDelete(req.params.id); const getCollection = await Collection.findByIdAndDelete(req.params.id);
@ -54,7 +75,12 @@ export const deleteCollection = async (req, res, next) => {
}; };
export const getCollections = async (req, res, next) => { export const getCollections = async (req, res, next) => {
try { try {
const getCollection = await Collection.find(); const getCollection = await Collection.find()
.populate({
path: "endpoints",
model: "Endpoint",
})
.exec();
// const Collection = getCollection.reverse(); // const Collection = getCollection.reverse();
if (getCollection) { if (getCollection) {
res.status(200).json(getCollection); res.status(200).json(getCollection);
@ -71,11 +97,9 @@ export const getCollection = async (req, res, next) => {
try { try {
const foundCollection = await Collection.findById(req.params.id) const foundCollection = await Collection.findById(req.params.id)
.populate({ .populate({
path: "userRef", path: "endpoints",
model: "User", model: "Endpoint",
// select: "fName lName username photo",
}) })
.exec(); .exec();
if (foundCollection) { if (foundCollection) {

View File

@ -66,19 +66,19 @@ export const getUserId = async (req, res) => {
export const updateUser = async (req, res, next) => { export const updateUser = async (req, res, next) => {
try { try {
const { const {
fName, // fName,
lName, // lName,
email, email,
phone, // phone,
address1, // address1,
address2, // address2,
city, // city,
province, // province,
country, // country,
zip, // zip,
type, // type,
status, // status,
transactions, // transactions,
} = req.body; } = req.body;
const emailTaken = await User.findOne({ email: email }); const emailTaken = await User.findOne({ email: email });
@ -101,20 +101,20 @@ export const updateUser = async (req, res, next) => {
}); });
if (!emailTaken && !taken) { if (!emailTaken && !taken) {
const newUser = { const newUser = {
fName, // fName,
lName, // lName,
email, email,
phone, // phone,
address1, // address1,
address2, // address2,
city, // city,
province, // province,
country, // country,
zip, // zip,
type, // type,
status, // status,
transactions, // transactions,
photo: result.secure_url, // photo: result.secure_url,
}; };
const updatedUser = await User.findByIdAndUpdate( const updatedUser = await User.findByIdAndUpdate(
req.params.id, req.params.id,
@ -130,18 +130,18 @@ export const updateUser = async (req, res, next) => {
} else if (!req.file) { } else if (!req.file) {
if (!emailTaken && !taken) { if (!emailTaken && !taken) {
const newUser = { const newUser = {
fName, // fName,
lName, // lName,
email, email,
phone, // phone,
address1, // address1,
address2, // address2,
city, // city,
province, // province,
country, // country,
zip, // zip,
transactions, // transactions,
status, // status,
}; };
const updatedUser = await User.findByIdAndUpdate( const updatedUser = await User.findByIdAndUpdate(
req.params.id, req.params.id,
@ -153,18 +153,18 @@ export const updateUser = async (req, res, next) => {
res.status(500).json({ error: "email taken" }); res.status(500).json({ error: "email taken" });
} else if (taken) { } else if (taken) {
const newUser = { const newUser = {
fName, // fName,
lName, // lName,
email, email,
phone, // phone,
address1, // address1,
address2, // address2,
city, // city,
province, // province,
country, // country,
zip, // zip,
transactions, // transactions,
status, // status,
}; };
const updatedUser = await User.findByIdAndUpdate( const updatedUser = await User.findByIdAndUpdate(
req.params.id, req.params.id,

View File

@ -21,6 +21,12 @@ const CollectionSchema = mongoose.Schema(
//required: true, //required: true,
}, },
], ],
endpoints: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Endpoint",
},
],
}, },
{ timestamps: true } { timestamps: true }
); );

View File

@ -18,12 +18,12 @@ const EndpointSchema = mongoose.Schema(
type: mongoose.Schema.Types.ObjectId, type: mongoose.Schema.Types.ObjectId,
ref: "Collection", ref: "Collection",
}, },
created_by: { // created_by: {
type: mongoose.Schema.Types.ObjectId, // type: mongoose.Schema.Types.ObjectId,
ref: "User", // ref: "User",
}, // },
body_json: { body_json: {
type: String, type: Array,
}, },
form_data: [ form_data: [
{ {

View File

@ -2,18 +2,18 @@ import mongoose from "mongoose";
const UserSchema = new mongoose.Schema( const UserSchema = new mongoose.Schema(
{ {
fName: { // fName: {
type: String, // type: String,
// required: true, // // required: true,
min: 2, // min: 2,
max: 50, // max: 50,
}, // },
lName: { // lName: {
type: String, // type: String,
// required: true, // // required: true,
min: 2, // min: 2,
max: 50, // max: 50,
}, // },
email: { email: {
type: String, type: String,
required: true, required: true,
@ -24,93 +24,93 @@ const UserSchema = new mongoose.Schema(
type: String, type: String,
// required: true, // required: true,
max: 30, max: 30,
unique: true, unique: false,
}, },
password: { password: {
type: String, type: String,
required: true, required: true,
min: 5, min: 5,
}, },
phone: { // phone: {
type: String, // type: String,
// // required: true,
// default: "",
// },
// photo: {
// type: String,
// },
// address1: {
// type: String,
// default: "",
// },
// address2: {
// type: String,
// default: "",
// },
// city: {
// type: String,
// default: "",
// },
// province: {
// type: String,
// default: "",
// },
// country: {
// type: String,
// default: "",
// },
// zip: {
// type: String,
// default: "",
// },
// type: {
// type: String,
// default: "client",
// },
// status: {
// type: String,
// default: "new",
// required: true, // required: true,
default: "", // },
}, // phoneStatus: {
photo: { // type: String,
type: String, // default: "new",
}, // // required: true,
address1: { // },
type: String, // kycStatus: {
default: "", // type: String,
}, // default: "new",
address2: { // // required: true,
type: String, // },
default: "", // kycRef: {
}, // type: mongoose.Schema.Types.ObjectId,
city: { // ref: "Kyc",
type: String, // },
default: "", // friends: [
}, // {
province: { // number: {
type: String, // type: String,
default: "", // required: false,
}, // },
country: { // favorite: {
type: String, // type: Boolean,
default: "", // required: false,
}, // default: false,
zip: { // },
type: String, // nickname: {
default: "", // type: String,
}, // required: false,
type: { // },
type: String, // },
default: "client", // ],
}, // otp: {
status: { // type: String,
type: String, // default: null,
default: "new", // },
required: true, // otpExpiration: {
}, // type: Date,
phoneStatus: { // default: null,
type: String, // },
default: "new",
// required: true,
},
kycStatus: {
type: String,
default: "new",
// required: true,
},
kycRef: {
type: mongoose.Schema.Types.ObjectId,
ref: "Kyc",
},
friends: [
{
number: {
type: String,
required: false,
},
favorite: {
type: Boolean,
required: false,
default: false,
},
nickname: {
type: String,
required: false,
},
},
],
otp: {
type: String,
default: null,
},
otpExpiration: {
type: Date,
default: null,
},
// transactions: { // transactions: {
// type: Array, // type: Array,

View File

@ -5,6 +5,7 @@ import {
deleteCollection, deleteCollection,
createCollection, createCollection,
updateCollection, updateCollection,
updateCollectionByEndpoint,
} from "../controllers/collection.js"; } from "../controllers/collection.js";
import { verifyToken } from "../middleware/auth.js"; import { verifyToken } from "../middleware/auth.js";
import logApiCall from "../middleware/log.js"; import logApiCall from "../middleware/log.js";
@ -26,20 +27,25 @@ router.get(
router.post( router.post(
"/create", "/create",
// verifyToken, // verifyToken,
logApiCall, // logApiCall,
createCollection createCollection
); );
router.patch( router.patch(
"/update/:id", "/update/:id",
// verifyToken, // verifyToken,
logApiCall, // logApiCall,
updateCollection updateCollection
); );
router.patch(
"/update/:id/endpoint",
// verifyToken,
// logApiCall,
updateCollectionByEndpoint
);
router.delete( router.delete(
"/:id/delete", "/:id/delete",
// verifyToken, // verifyToken,
logApiCall, // logApiCall,
deleteCollection deleteCollection
); );

View File

@ -28,20 +28,20 @@ router.get(
router.post( router.post(
"/create", "/create",
// verifyToken, // verifyToken,
logApiCall, // logApiCall,
createEndpoint createEndpoint
); );
router.patch( router.patch(
"/update/:id", "/update/:id",
// verifyToken, // verifyToken,
logApiCall, // logApiCall,
updateEndpoint updateEndpoint
); );
router.delete( router.delete(
"/:id/delete", "/:id/delete",
// verifyToken, // verifyToken,
logApiCall, // logApiCall,
deleteEndpoint deleteEndpoint
); );