
* Add vital integration * Tidy up client_user_id creation * Rename vital app to vitalother to follow name rules * Added env var * App vital reschedule * Fix on app structure and api calls * Implemented user identification from webhook * WIP fix api call and read me * Save vital settings via api * Now saving userVitalSettings and trigger reschedule on selected param * Added translations * Fix type for vitalSettings * Using api to get env vars required for url, fix display of vital settings * Fix hours placeholder, translation not working * Renames vital app * Update seed-app-store.ts * Update package.json * Update yarn.lock * Refactored env variables * Update README.md * Migrates to api_keys * Extracts AppConfiguration * vitalClient fixes * Update index.ts * Update metadata.ts * Update index.ts * Update metadata.ts * Added namespace vital for translations Co-authored-by: Maitham <maithamdib@gmail.com> Co-authored-by: zomars <zomars@me.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
91 lines
2.2 KiB
TypeScript
91 lines
2.2 KiB
TypeScript
import { Prisma } from "@prisma/client";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { z, ZodError } from "zod";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
export type VitalSettingsResponse = {
|
|
connected: boolean;
|
|
sleepValue: number;
|
|
selectedParam: string;
|
|
};
|
|
|
|
const vitalSettingsUpdateSchema = z.object({
|
|
connected: z.boolean().optional(),
|
|
selectedParam: z.string().optional(),
|
|
sleepValue: z.number().optional(),
|
|
});
|
|
|
|
const handler = async (
|
|
req: NextApiRequest,
|
|
res: NextApiResponse
|
|
): Promise<VitalSettingsResponse | NextApiResponse | void> => {
|
|
if (req.method === "PUT" && req.session && req.session.user.id) {
|
|
const userId = req.session.user.id;
|
|
const body = req.body;
|
|
try {
|
|
const userWithMetadata = await prisma.user.findFirst({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
select: {
|
|
id: true,
|
|
metadata: true,
|
|
},
|
|
});
|
|
const userMetadata = userWithMetadata?.metadata as Prisma.JsonObject;
|
|
const vitalSettings =
|
|
((userWithMetadata?.metadata as Prisma.JsonObject)?.vitalSettings as Prisma.JsonObject) || {};
|
|
await prisma.user.update({
|
|
where: {
|
|
id: userId,
|
|
},
|
|
data: {
|
|
metadata: {
|
|
...userMetadata,
|
|
vitalSettings: {
|
|
...vitalSettings,
|
|
...body,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (vitalSettings) {
|
|
res.status(200).json(vitalSettings);
|
|
} else {
|
|
res.status(404);
|
|
}
|
|
} catch (error) {
|
|
res.status(500);
|
|
}
|
|
} else {
|
|
res.status(400);
|
|
}
|
|
res.end();
|
|
};
|
|
|
|
function validate(
|
|
handler: (
|
|
req: NextApiRequest,
|
|
res: NextApiResponse
|
|
) => Promise<VitalSettingsResponse | NextApiResponse | void>
|
|
) {
|
|
return async (req: NextApiRequest, res: NextApiResponse) => {
|
|
if (req.method === "POST" || req.method === "PUT") {
|
|
try {
|
|
vitalSettingsUpdateSchema.parse(req.body);
|
|
} catch (error) {
|
|
if (error instanceof ZodError && error?.name === "ZodError") {
|
|
return res.status(400).json(error?.issues);
|
|
}
|
|
return res.status(402);
|
|
}
|
|
} else {
|
|
return res.status(405);
|
|
}
|
|
await handler(req, res);
|
|
};
|
|
}
|
|
|
|
export default validate(handler);
|