Skip to content

Commit

Permalink
chore: Update clients to user-scoped or public-scoped where practical
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Sep 20, 2023
1 parent 5e069db commit ab0a604
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 22 deletions.
5 changes: 3 additions & 2 deletions api.planx.uk/admin/session/bops.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response, NextFunction } from "express";
import { $admin } from "../../client";
import { getClient } from "../../client";

/**
* @swagger
Expand All @@ -20,7 +20,8 @@ export const getBOPSPayload = async (
next: NextFunction,
) => {
try {
const { exportData } = await $admin.export.bopsPayload(
const $client = getClient();
const { exportData } = await $client.export.bopsPayload(
req.params.sessionId,
);
res.set("content-type", "application/json");
Expand Down
8 changes: 5 additions & 3 deletions api.planx.uk/admin/session/csv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { stringify } from "csv-stringify";
import { NextFunction, Request, Response } from "express";
import { $admin } from "../../client";
import { getClient } from "../../client";

/**
* @swagger
Expand All @@ -26,7 +26,8 @@ export async function getCSVData(
next: NextFunction,
) {
try {
const { responses } = await $admin.export.csvData(req.params.sessionId);
const $client = getClient();
const { responses } = await $client.export.csvData(req.params.sessionId);

if (req.query?.download) {
stringify(responses, {
Expand Down Expand Up @@ -69,7 +70,8 @@ export async function getRedactedCSVData(
next: NextFunction,
) {
try {
const { redactedResponses } = await $admin.export.csvData(
const $client = getClient();
const { redactedResponses } = await $client.export.csvData(
req.params.sessionId,
);

Expand Down
12 changes: 7 additions & 5 deletions api.planx.uk/admin/session/html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateApplicationHTML } from "@opensystemslab/planx-core";
import { $admin } from "../../client";
import { getClient } from "../../client";
import type { RequestHandler } from "express";
import type { PlanXExportData } from "@opensystemslab/planx-core/types";

Expand All @@ -20,10 +20,11 @@ type HTMLExportHandler = RequestHandler<{ sessionId: string }, string>;
*/
export const getHTMLExport: HTMLExportHandler = async (req, res, next) => {
try {
const session = await $admin.session.find(req.params.sessionId);
const $client = getClient();
const session = await $client.session.find(req.params.sessionId);
if (!session) throw Error(`Unable to find session ${req.params.sessionId}`);

const { responses } = await $admin.export.csvData(req.params.sessionId);
const { responses } = await $client.export.csvData(req.params.sessionId);
const boundingBox =
session.data.passport.data["property.boundary.site.buffered"];

Expand Down Expand Up @@ -60,10 +61,11 @@ export const getRedactedHTMLExport: HTMLExportHandler = async (
next,
) => {
try {
const session = await $admin.session.find(req.params.sessionId);
const $client = getClient();
const session = await $client.session.find(req.params.sessionId);
if (!session) throw Error(`Unable to find session ${req.params.sessionId}`);

const { redactedResponses } = await $admin.export.csvData(
const { redactedResponses } = await $client.export.csvData(
req.params.sessionId,
);
const boundingBox =
Expand Down
4 changes: 2 additions & 2 deletions api.planx.uk/inviteToPay/sendConfirmationEmail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $admin } from "../client";
import { $public, $admin } from "../client";
import { sendEmail } from "../notify";
import { gql } from "graphql-request";
import { convertSlugToName } from "../saveAndReturn/utils";
Expand All @@ -8,7 +8,7 @@ export async function sendAgentAndPayeeConfirmationEmail(sessionId: string) {
const { personalisation, applicantEmail, payeeEmail, projectTypes } =
await getDataForPayeeAndAgentEmails(sessionId);
const projectType = projectTypes.length
? await $admin.formatRawProjectTypes(projectTypes)
? await $public.formatRawProjectTypes(projectTypes)
: "Project type not submitted";
const config: AgentAndPayeeSubmissionNotifyConfig = {
personalisation: {
Expand Down
4 changes: 2 additions & 2 deletions api.planx.uk/inviteToPay/sendPaymentEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Template, getClientForTemplate, sendEmail } from "../notify";
import { InviteToPayNotifyConfig } from "../types";
import { Team } from "../types";
import type { PaymentRequest } from "@opensystemslab/planx-core/types";
import { $admin } from "../client";
import { $public } from "../client";

interface SessionDetails {
email: string;
Expand Down Expand Up @@ -118,7 +118,7 @@ const getInviteToPayNotifyConfig = async (
).title,
fee: getFee(paymentRequest),
projectType:
(await $admin.formatRawProjectTypes(
(await $public.formatRawProjectTypes(
paymentRequest.sessionPreviewData?.["proposal.projectType"] as string[],
)) || "Project type not submitted",
serviceName: convertSlugToName(session.flow.slug),
Expand Down
11 changes: 7 additions & 4 deletions api.planx.uk/modules/team/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { $admin } from "../../client";
import { getClient } from "../../client";
import { ValidatedRequestHandler } from "../../shared/middleware/validate";

interface TeamMemberResponse {
Expand Down Expand Up @@ -39,7 +39,8 @@ export const addMember: UpsertMember = async (req, res) => {
const { teamId } = req.params;
const { userId, role } = req.body;

const isSuccess = await $admin.team.addMember({ teamId, userId, role });
const $client = getClient();
const isSuccess = await $client.team.addMember({ teamId, userId, role });

if (!isSuccess)
return res.status(500).json({ message: "Failed to add member to team" });
Expand All @@ -51,7 +52,8 @@ export const changeMemberRole: UpsertMember = async (req, res) => {
const { teamId } = req.params;
const { userId, role } = req.body;

const isSuccess = await $admin.team.changeMemberRole({
const $client = getClient();
const isSuccess = await $client.team.changeMemberRole({
teamId,
userId,
role,
Expand All @@ -67,7 +69,8 @@ export const removeMember: RemoveMember = async (req, res) => {
const { teamId } = req.params;
const { userId } = req.body;

const isSuccess = await $admin.team.removeMember({ teamId, userId });
const $client = getClient();
const isSuccess = await $client.team.removeMember({ teamId, userId });

if (!isSuccess)
return res
Expand Down
4 changes: 2 additions & 2 deletions api.planx.uk/saveAndReturn/resumeApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LowCalSession, Team } from "../types";
import { convertSlugToName, getResumeLink, calculateExpiryDate } from "./utils";
import { sendEmail } from "../notify";
import type { SiteAddress } from "@opensystemslab/planx-core/types";
import { $admin } from "../client";
import { $public } from "../client";

/**
* Send a "Resume" email to an applicant which list all open applications for a given council (team)
Expand Down Expand Up @@ -121,7 +121,7 @@ const buildContentFromSessions = async (
const address: SiteAddress | undefined =
session.data?.passport?.data?._address;
const addressLine = address?.single_line_address || address?.title;
const projectType = await $admin.formatRawProjectTypes(
const projectType = await $public.formatRawProjectTypes(
session.data?.passport?.data?.["proposal.projectType"],
);
const resumeLink = getResumeLink(session, team, session.flow.slug);
Expand Down
4 changes: 2 additions & 2 deletions api.planx.uk/saveAndReturn/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { gql } from "graphql-request";
import { adminGraphQLClient as adminClient } from "../hasura";
import { LowCalSession, Team } from "../types";
import { Template, getClientForTemplate, sendEmail } from "../notify";
import { $admin } from "../client";
import { $public } from "../client";

const DAYS_UNTIL_EXPIRY = 28;
const REMINDER_DAYS_FROM_EXPIRY = [7, 1];
Expand Down Expand Up @@ -145,7 +145,7 @@ const getSessionDetails = async (
session.data.passport?.data?.["proposal.projectType"];
const projectTypes =
passportProtectTypes &&
(await $admin.formatRawProjectTypes(passportProtectTypes));
(await $public.formatRawProjectTypes(passportProtectTypes));
const address: SiteAddress | undefined =
session.data?.passport?.data?._address;
const addressLine = address?.single_line_address || address?.title;
Expand Down

0 comments on commit ab0a604

Please sign in to comment.