mirror of
https://github.com/vitalityAI/therapist.git
synced 2025-04-08 05:40:17 -04:00
fix: handled operator db call errors
This commit is contained in:
parent
a38b84a84c
commit
4bf903eede
|
@ -7,38 +7,47 @@ dotenv.config();
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
app.post("/", async (req, res) => {
|
app.post("/", async (req, res) => {
|
||||||
const operator = await db.operator.create({
|
try {
|
||||||
data: {
|
const operator = await db.operator.create({
|
||||||
name: req.body.name,
|
data: {
|
||||||
phoneNumber: req.body.phoneNumber,
|
name: req.body.name,
|
||||||
email: req.body.email
|
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({
|
return res.status(200).json({
|
||||||
operator
|
operator,
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/update", async (req, res) => {
|
app.post("/update", async (req, res) => {
|
||||||
const {authorized, webSession} = await authorize(req, res)
|
const { authorized, webSession } = await authorize(req, res);
|
||||||
if(!authorized) return res.status(401).send(null)
|
if (!authorized) return res.status(401).send(null);
|
||||||
|
try {
|
||||||
const operator = await db.operator.update({
|
const operator = await db.operator.update({
|
||||||
where: {
|
where: {
|
||||||
id: webSession.operatorId
|
id: webSession.operatorId,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
phoneNumber: req.body.phone
|
phoneNumber: req.body.phone,
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
return res.status(500).send("Operator update failed.");
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).json({
|
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 email = req.body.session.user.email;
|
||||||
const name = req.body.session.user.name;
|
const name = req.body.session.user.name;
|
||||||
const token = req.body.token.sub;
|
const token = req.body.token.sub;
|
||||||
|
@ -46,49 +55,53 @@ app.post("/login", async (req, res) => {
|
||||||
let operator = await db.operator.findUnique({
|
let operator = await db.operator.findUnique({
|
||||||
where: {
|
where: {
|
||||||
email: email,
|
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({
|
webSession = await db.webSession.create({
|
||||||
data: {
|
data: {
|
||||||
operatorId: operator.id,
|
operatorId: operator.id,
|
||||||
token: token
|
token: token,
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
operator,
|
operator,
|
||||||
webSession
|
webSession,
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/', async (req, res) => {
|
app.get("/", async (req, res) => {
|
||||||
const email = req.query.email
|
const email = req.query.email;
|
||||||
const operator = await db.operator.findUnique({
|
const operator = await db.operator.findUnique({
|
||||||
where: {
|
where: {
|
||||||
email: email
|
email: email,
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
return res.status(200).json({operator})
|
return res.status(200).json({ operator });
|
||||||
})
|
});
|
||||||
|
|
||||||
|
export default app;
|
||||||
export default app
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user