Skip to content

Commit

Permalink
Latest changes, posting and deleting should work now
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinLemon112 committed Mar 10, 2024
1 parent 1b90494 commit 7243d22
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/notification/publish/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ const topic = process.env.ACTIVE_MQ_TOPIC as string; // Type assertion
server.use(express.json());
// MongoDB event schema and model
interface accounts extends Document {
creationDate: Date;
deletionDate?: Date;
lastUpdated: Date; // Making lastUpdated required
email: string;
name: string;
devicesLinked?: {};
}

const accountSchema = new Schema<accounts>({
email: String,
name: String,
creationDate: { type: Date, required: true },
deletionDate: { type: Date },
lastUpdated: { type: Date, required: true }, // Making lastUpdated required
email: { type: String, required: true },
name: { type: String, required: true },
devicesLinked: { type: Schema.Types.Mixed }
});

// Establish Mongoose connection
Expand Down Expand Up @@ -73,14 +81,21 @@ mongoose.connect(db.dbURI, {

server.post("/publish/accounts/new", async (req, res) => {
try {
const eventData = new EventModel({
email: req.body.email,
name: req.body.name,
});
const { email, name, deletionDate, devicesLinked } = req.body;
const currentDate = new Date(); // Set current timestamp
const newUserData = {
email,
name,
creationDate: currentDate,
lastUpdated: currentDate,
deletionDate: deletionDate ? new Date(deletionDate) : null,
devicesLinked: devicesLinked ? devicesLinked : []
};

const newEvent = await eventData.save(); // Save the new document to the database
res.json(newEvent);
const newUser = new EventModel(newUserData);

const savedUser = await newUser.save(); // Save the new user to the database
res.status(201).json(savedUser); // HTTP 201 Created status code for successful creation
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
Expand Down

0 comments on commit 7243d22

Please sign in to comment.