fix: handled operator db call errors

This commit is contained in:
Rushil Umaretiya 2023-03-06 09:34:24 -05:00
parent a38b84a84c
commit 4bf903eede

View File

@ -7,38 +7,47 @@ dotenv.config();
const app = Router();
app.post("/", async (req, res) => {
const operator = await db.operator.create({
data: {
name: req.body.name,
phoneNumber: req.body.phoneNumber,
email: req.body.email
}
})
try {
const operator = await db.operator.create({
data: {
name: req.body.name,
phoneNumber: req.body.phoneNumber,
email: req.body.email,
},
});
} catch (e) {
console.log(e);
res.status(500).send("Operator creation failed.");
}
return res.status(200).json({
operator
})
operator,
});
});
app.post("/update", async (req, res) => {
const {authorized, webSession} = await authorize(req, res)
if(!authorized) return res.status(401).send(null)
const operator = await db.operator.update({
where: {
id: webSession.operatorId
},
data: {
phoneNumber: req.body.phone
}
})
const { authorized, webSession } = await authorize(req, res);
if (!authorized) return res.status(401).send(null);
try {
const operator = await db.operator.update({
where: {
id: webSession.operatorId,
},
data: {
phoneNumber: req.body.phone,
},
});
} catch (e) {
console.log(e);
return res.status(500).send("Operator update failed.");
}
return res.status(200).json({
operator
})
})
operator,
});
});
app.post("/login", async (req, res) => {
app.post("/login", async (req, res) => {
const email = req.body.session.user.email;
const name = req.body.session.user.name;
const token = req.body.token.sub;
@ -46,49 +55,53 @@ app.post("/login", async (req, res) => {
let operator = await db.operator.findUnique({
where: {
email: email,
}
})
if(!operator){
operator = await db.operator.create({
data: {
name: name,
email: email
}
})
}
let webSession = await db.webSession.findUnique({
where: {
operatorId: operator.id
}
},
});
if(!webSession){
if (!operator) {
try {
operator = await db.operator.create({
data: {
name: name,
email: email,
},
});
} catch (e) {
console.log(e);
return res.status(500).send("Operator creation failed.");
}
}
let webSession = await db.webSession.findUnique({
where: {
operatorId: operator.id,
},
});
if (!webSession) {
webSession = await db.webSession.create({
data: {
operatorId: operator.id,
token: token
}
})
token: token,
},
});
}
return res.status(200).json({
operator,
webSession
})
webSession,
});
});
app.get('/', async (req, res) => {
const email = req.query.email
app.get("/", async (req, res) => {
const email = req.query.email;
const operator = await db.operator.findUnique({
where: {
email: email
}
})
email: email,
},
});
return res.status(200).json({operator})
})
return res.status(200).json({ operator });
});
export default app
export default app;