* feat: installing the app works * Update yarn.lock * feat: /api/callback now gets user auth info from basecamp * feat: updated basecamp logo * feat: added project dropdown on event apps page * feat: basecamp event creation and deletion working * feat: basecamp event rescheduling now works * refactor(CalendarService): basecamp CaldendarService code clean up * refactor: code cleanup for basecamp app API * feat: updated event summary text sent to basecamp * chore: updated basecamp images and contact info * fix: fixed typescript errors and added logic to refresh tokens on event settings * refactor(CaldendarService): used refreshAccessToken from helpers.ts instead * chore: updated basecamp description * fix: fixed incorrect import * fix: accidentally deleted props to toggle app for event * chore: updated .env.appStore.example and added README for app * Update .env.appStore.example Co-authored-by: Leo Giovanetti <hello@leog.me> * feat: added basecamp userAgent in env instead of hardcoded value * feat: updated README to include how to set basecamp user agent env * fix: removed unused import * feat: used URLSearchParams to construct url params * fix: fixed typescript errors * chore: updated README to include an example on how to set basecamp user-agent * feat: using TRPC instead of REST * chore: removed old projects REST code --------- Co-authored-by: Leo Giovanetti <hello@leog.me>
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import type { PrismaClient } from "@calcom/prisma/client";
|
|
import { TRPCError } from "@calcom/trpc/server";
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
|
|
|
|
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
|
import type { BasecampToken } from "../lib/CalendarService";
|
|
import { refreshAccessToken } from "../lib/helpers";
|
|
|
|
interface ProjectsHandlerOptions {
|
|
ctx: {
|
|
prisma: PrismaClient;
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
}
|
|
|
|
export const projectHandler = async ({ ctx }: ProjectsHandlerOptions) => {
|
|
const { user_agent } = await getAppKeysFromSlug("basecamp3");
|
|
const { user, prisma } = ctx;
|
|
const credential = await prisma.credential.findFirst({
|
|
where: {
|
|
userId: user?.id,
|
|
},
|
|
});
|
|
if (!credential) {
|
|
throw new TRPCError({ code: "FORBIDDEN", message: "No credential found for user" });
|
|
}
|
|
let credentialKey = credential.key as BasecampToken;
|
|
if (credentialKey.expires_at < Date.now()) {
|
|
credentialKey = (await refreshAccessToken(credential)) as BasecampToken;
|
|
}
|
|
|
|
const url = `${credentialKey.account.href}/projects.json`;
|
|
|
|
const resp = await fetch(url, {
|
|
headers: { "User-Agent": user_agent as string, Authorization: `Bearer ${credentialKey.access_token}` },
|
|
});
|
|
const projects = await resp.json();
|
|
return { currentProject: credentialKey.projectId, projects };
|
|
};
|