Files
smarthome/controller/device.log.controller.js
2024-03-05 00:26:10 +03:00

48 lines
1020 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const configs = require("../configs");
const DeviceModel = require("../models/device.model");
const set = async (
token,
device_id,
remote_name,
remote_type,
operating_type
) => {
if (token === configs.verifyToken) {
try {
// Önce kaydı bulalım
const filter = {
_id: device_id
};
const update = {
$push: {
device_log: {
remote_name,
remote_type,
operating_type,
log_time: Date.now()
}
}
};
const result = await DeviceModel.updateOne(filter, update);
if (result.nModified === 0) {
throw new Error("Güncellenmek istenen kayıt bulunamadı");
}
return "Log Kaydedildi!";
} catch (error) {
// if (error.code === 11000) {
// throw new Error("Zaten kaydedilmiş");
// }
throw new Error("Mongodb'ye kaydedilirken hata oluştu");
}
} else {
throw new Error("Hatalı token");
}
};
module.exports = { set };