Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,104 @@
|
||||
import { ActionFunction, LoaderFunctionArgs } from "react-router";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getOnboardingSession, OnboardingSessionWithDetails } from "~/modules/onboarding/db/onboardingSessions.db.server";
|
||||
import { OnboardingSessionActionDto } from "~/modules/onboarding/dtos/OnboardingSessionActionDto";
|
||||
import OnboardingSessionService from "~/modules/onboarding/services/OnboardingSessionService";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
|
||||
type LoaderData = {
|
||||
item: OnboardingSessionWithDetails | null;
|
||||
};
|
||||
|
||||
type ServerT = Awaited<ReturnType<typeof getTranslations>>["t"];
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const item = await getOnboardingSession(params.id!);
|
||||
const unauthorized = await requireOnboardingSessionOwner({ request, session: item, t });
|
||||
if (unauthorized) {
|
||||
throw unauthorized;
|
||||
}
|
||||
const data: LoaderData = {
|
||||
item,
|
||||
};
|
||||
return data;
|
||||
};
|
||||
|
||||
export const action: ActionFunction = async ({ request, params }) => {
|
||||
const { t } = await getTranslations(request);
|
||||
const sessionId = params.id!;
|
||||
const session = await getOnboardingSession(sessionId);
|
||||
if (!session) {
|
||||
return Response.json({ error: t("onboarding.session.errors.notFound") }, { status: 404 });
|
||||
}
|
||||
|
||||
const userInfo = await getUserInfo(request);
|
||||
if (!userInfo.userId || session.userId !== userInfo.userId) {
|
||||
return Response.json({ error: t("onboarding.session.errors.unauthorized") }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
await validateCSRFToken(request);
|
||||
} catch (error) {
|
||||
return Response.json({ error: error instanceof Error ? error.message : t("errors:invalidCsrfToken") }, { status: 400 });
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
const actionName = formData.get("action");
|
||||
const actions = formData.getAll("actions[]").map((action: FormDataEntryValue) => {
|
||||
return JSON.parse(action.toString()) as OnboardingSessionActionDto;
|
||||
});
|
||||
switch (actionName) {
|
||||
case "started": {
|
||||
await OnboardingSessionService.started({ session, request });
|
||||
break;
|
||||
}
|
||||
case "dismissed": {
|
||||
await OnboardingSessionService.dismissed({ session, request });
|
||||
break;
|
||||
}
|
||||
case "add-actions": {
|
||||
await OnboardingSessionService.addActions(session, { actions });
|
||||
break;
|
||||
}
|
||||
case "set-step": {
|
||||
await OnboardingSessionService.setStep(session, {
|
||||
fromIdx: Number(formData.get("fromIdx")),
|
||||
toIdx: Number(formData.get("toIdx")),
|
||||
actions,
|
||||
});
|
||||
break;
|
||||
}
|
||||
case "complete": {
|
||||
await OnboardingSessionService.complete({
|
||||
session,
|
||||
data: { fromIdx: Number(formData.get("fromIdx")), actions },
|
||||
request,
|
||||
});
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
return Response.json({ error: t("shared.invalidForm") }, { status: 400 });
|
||||
}
|
||||
}
|
||||
return Response.json({});
|
||||
};
|
||||
|
||||
async function requireOnboardingSessionOwner({
|
||||
request,
|
||||
session,
|
||||
t,
|
||||
}: {
|
||||
request: Request;
|
||||
session: OnboardingSessionWithDetails | null;
|
||||
t: ServerT;
|
||||
}): Promise<Response | undefined> {
|
||||
if (!session) {
|
||||
return Response.json({ error: t("onboarding.session.errors.notFound") }, { status: 404 });
|
||||
}
|
||||
|
||||
const userInfo = await getUserInfo(request);
|
||||
if (!userInfo.userId || session.userId !== userInfo.userId) {
|
||||
return Response.json({ error: t("onboarding.session.errors.unauthorized") }, { status: 401 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user