28 lines
856 B
JavaScript
28 lines
856 B
JavaScript
import PropertyModal from "../models/property.js";
|
|
import UserModal from "../models/user.js";
|
|
import mongoose from "mongoose";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
|
|
export const createProperty = async (req, res) => {
|
|
const property = req.body;
|
|
function generateRandomNumber() {
|
|
return Math.floor(Math.random() * 90000) + 10000;
|
|
}
|
|
const randomNumber = generateRandomNumber().toString();
|
|
const propertyId = `EFP${randomNumber}`;
|
|
const newProperty = new PropertyModal({
|
|
...property,
|
|
creator: req.userId,
|
|
createdAt: new Date().toISOString(),
|
|
publishedAt: new Date().toISOString(),
|
|
currentYear: new Date().getFullYear(),
|
|
propertyId: propertyId,
|
|
});
|
|
|
|
try {
|
|
await newProperty.save();
|
|
res.status(201).json(newProperty);
|
|
} catch (error) {
|
|
res.status(404).json({ message: "Something went wrong" });
|
|
}
|
|
}; |