@coursebuilder/core
Version:
Core package for Course Builder
267 lines (265 loc) • 7.55 kB
JavaScript
import {
processStripeWebhook
} from "./chunk-7JAQB4AI.js";
import {
VIDEO_TRANSCRIPT_READY_EVENT
} from "./chunk-QLPBMM7X.js";
import {
VIDEO_SRT_READY_EVENT
} from "./chunk-FEG4FJXK.js";
import {
sendServerEmail
} from "./chunk-ZJOFKIX6.js";
import {
filterNullFields,
getConvertkitSubscriberCookie
} from "./chunk-YG7SUWCG.js";
import {
srtFromTranscriptResult,
transcriptAsParagraphsWithTimestamps,
wordLevelSrtFromTranscriptResult
} from "./chunk-C7YJXK7J.js";
import {
z
} from "./chunk-JLNB6NRA.js";
import {
__name
} from "./chunk-VLQXSCFN.js";
// src/lib/actions/index.ts
async function getUserPurchases(request, cookies, options) {
const client = options.adapter;
const currentUser = await options.getCurrentUser?.();
if (!currentUser?.id || currentUser.id !== request.query?.userId) {
return {
status: 401,
body: [],
headers: {
"Content-Type": "application/json"
},
cookies
};
}
if (!request.query?.userId)
return {
status: 200,
body: [],
headers: {
"Content-Type": "application/json"
},
cookies
};
const purchases = await client?.getPurchasesForUser(request.query.userId);
if (!purchases) {
return {
status: 200,
body: [],
headers: {
"Content-Type": "application/json"
},
cookies
};
} else {
return {
status: 200,
body: purchases,
headers: {
"Content-Type": "application/json"
},
cookies
};
}
}
__name(getUserPurchases, "getUserPurchases");
async function getSubscriber(options, cookies) {
switch (options.provider.type) {
case "email-list":
const subscriber = await options.provider.getSubscriber(options.url.searchParams.get("subscriberId") || options.cookies.ck_subscriber_id || null);
return {
status: 200,
body: subscriber,
headers: {
"Content-Type": "application/json"
},
cookies: getConvertkitSubscriberCookie(subscriber)
};
default:
throw new Error("Unsupported provider");
}
}
__name(getSubscriber, "getSubscriber");
async function subscribeToList(request, cookies, options) {
const response = {
body: null,
headers: {
"Content-Type": "application/json"
},
cookies
};
switch (options.provider.type) {
case "email-list":
const subscribeOptions = z.object({
listId: z.union([
z.number(),
z.string()
]).optional(),
listType: z.string().optional(),
fields: z.record(z.string(), z.any()).optional().nullable(),
email: z.string(),
name: z.string().optional().nullable()
}).parse(request.body);
let user = await options.adapter?.getUserByEmail(subscribeOptions.email);
if (!user) {
user = await options.adapter?.createUser({
id: crypto.randomUUID(),
email: subscribeOptions.email,
name: subscribeOptions.name,
emailVerified: null
});
if (!user)
throw new Error("Could not create user");
options.logger.debug(`created user ${user.id}`);
} else {
options.logger.debug(`found user ${user.id}`);
}
response.body = await options.provider.subscribeToList({
user,
listId: subscribeOptions.listId || options.provider.defaultListId,
fields: subscribeOptions.fields || {},
listType: subscribeOptions.listType || options.provider.defaultListType
});
response.cookies = getConvertkitSubscriberCookie(filterNullFields(response.body));
if (!user.emailVerified) {
const emailProvider = options.providers.find((p) => p.type === "email");
if (emailProvider && options.adapter && options.provider.id === "coursebuilder") {
await sendServerEmail({
email: user.email,
type: "signup",
callbackUrl: `${options.baseUrl}/confirmed`,
emailProvider,
authOptions: options.authConfig,
adapter: options.adapter,
baseUrl: options.baseUrl
});
}
}
}
return response;
}
__name(subscribeToList, "subscribeToList");
async function session(options, cookies) {
const { callbacks, logger } = options;
const response = {
body: null,
headers: {
"Content-Type": "application/json"
},
cookies
};
try {
response.body = await callbacks.session({});
} catch (e) {
logger.error(e);
}
return response;
}
__name(session, "session");
async function srt(request, cookies, options) {
const client = options.adapter;
const resource = await client?.getContentResource(request.query?.videoResourceId);
if (!resource)
throw new Error("Resource not found");
return {
status: 200,
body: resource.fields?.srt,
headers: {
"Content-Type": "application/text"
},
cookies
};
}
__name(srt, "srt");
async function webhook(request, cookies, options) {
if (!options.provider)
throw new Error("Provider not found");
switch (options.provider.type) {
case "payment":
if (options.provider.id === "stripe") {
await processStripeWebhook(request.body, options);
return Response.json("ok", {
status: 200
});
} else {
throw new Error("Unsupported provider");
}
case "transcription":
if (!request.body)
throw new Error("No body");
const { results } = request.body;
const videoResourceId = options.url.searchParams.get("videoResourceId");
if (!videoResourceId)
throw new Error("No videoResourceId");
const videoResource = await options.adapter?.getContentResource(videoResourceId);
if (!videoResource)
throw new Error("No videoResource");
const rawTranscriptId = `raw-transcript-${videoResourceId}`;
const existingRawTranscript = await options.adapter?.getContentResource(rawTranscriptId);
if (!existingRawTranscript) {
await options.adapter?.createContentResource({
id: rawTranscriptId,
type: "raw-transcript",
fields: {
deepgramResults: results
},
createdById: videoResource.createdById
});
await options.adapter?.addResourceToResource({
childResourceId: rawTranscriptId,
parentResourceId: videoResourceId
});
}
const srt2 = srtFromTranscriptResult(results);
const wordLevelSrt = wordLevelSrtFromTranscriptResult(results);
const transcript = transcriptAsParagraphsWithTimestamps(results);
await options.adapter?.updateContentResourceFields({
id: videoResourceId,
fields: {
transcript,
srt: srt2,
wordLevelSrt
}
});
await options.inngest.send({
name: VIDEO_TRANSCRIPT_READY_EVENT,
data: {
videoResourceId
}
});
if (srt2 && wordLevelSrt && videoResourceId) {
await options.inngest.send({
name: VIDEO_SRT_READY_EVENT,
data: {
videoResourceId
}
});
}
return {
status: 200,
body: null,
headers: {
"Content-Type": "application/json"
},
cookies
};
}
throw new Error("Invalid provider type");
}
__name(webhook, "webhook");
export {
getUserPurchases,
getSubscriber,
subscribeToList,
session,
srt,
webhook
};
//# sourceMappingURL=chunk-CNLQWUTB.js.map