Fix credential creation flow for msteams app (#3029)

* Fix credential creation flow for msteams app

* Code review improve

* Fix preffer early returns

Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/3063/head^2
alannnc 2022-06-14 20:42:13 -06:00 committed by GitHub
parent eb64904ba3
commit 041ad9cd94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View File

@ -65,11 +65,38 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
responseBody.expiry_date = Math.round(+new Date() / 1000 + responseBody.expires_in); // set expiry date in seconds
delete responseBody.expires_in;
const userId = req.session?.user.id;
if (!userId) {
return res.status(404).json({ message: "No user found" });
}
/**
* With this we take care of no duplicate office365_video key for a single user
* when creating a video room we only do findFirst so the if they have more than 1
* others get ignored
* */
const existingCredentialOfficeVideo = await prisma.credential.findMany({
select: {
id: true,
},
where: {
type: "office365_video",
userId: req.session?.user.id,
appId: "msteams",
},
});
// Making sure we only delete office365_video
const credentialIdsToDelete = existingCredentialOfficeVideo.map((item) => item.id);
if (credentialIdsToDelete.length > 0) {
await prisma.credential.deleteMany({ where: { id: { in: credentialIdsToDelete }, userId } });
}
await prisma.credential.create({
data: {
type: "office365_video",
key: responseBody,
userId: req.session?.user.id,
userId,
appId: "msteams",
},
});

View File

@ -49,7 +49,7 @@ const o365Auth = async (credential: Credential) => {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
scope: "User.Read Calendars.Read Calendars.ReadWrite",
scope: "User.Read OnlineMeetings.ReadWrite",
client_id,
refresh_token: refreshToken,
grant_type: "refresh_token",