Skip to content

Commit

Permalink
[support] Can edit PkgVersion JSON bundle in /admin
Browse files Browse the repository at this point in the history
Change-Id: I30011e5d3a75d9c33a411029003cf1c70b634985
GitOrigin-RevId: aa4d2a1884ad1c90fc2cf0f6df28e5b2d18f5ab4
  • Loading branch information
chungwu authored and Copybara committed Nov 28, 2023
1 parent 444c8eb commit d402189
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 3 deletions.
96 changes: 93 additions & 3 deletions platform/wab/src/wab/client/components/pages/AdminPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
TeamId,
TeamWhiteLabelInfo,
} from "@/wab/shared/ApiSchema";
import { PkgVersionInfo } from "@/wab/shared/SharedApi";
import {
Button,
Checkbox,
Expand Down Expand Up @@ -2261,15 +2262,15 @@ function DownloadAppMeta() {
);
}

function EditBundle() {
function EditProjectRevBundle() {
const nonAuthCtx = useNonAuthCtx();
const [initialRev, setInitialRev] = React.useState<
ApiProjectRevision | undefined
>(undefined);
const editorRef = React.useRef<FullCodeEditor>(null);
return (
<div>
<h1>Edit bundle</h1>
<h1>Edit ProjectRevision bundle</h1>
<Form
onFinish={async (values) => {
const projectId = values.projectId;
Expand Down Expand Up @@ -2333,6 +2334,94 @@ function EditBundle() {
);
}

function EditPkgVersionBundle() {
const nonAuthCtx = useNonAuthCtx();
const [pkgVersion, setPkgVersion] = React.useState<
PkgVersionInfo | undefined
>(undefined);
const editorRef = React.useRef<FullCodeEditor>(null);
return (
<div>
<h1>Edit PkgVersion bundle</h1>
<p>Look up either by Pkg ID or PkgVersion ID</p>
<Form
onFinish={async (values) => {
const res = await nonAuthCtx.api.getPkgVersionAsAdmin({
pkgId: values.pkgId,
version:
values.version?.trim()?.length > 0
? values.version.trim()
: undefined,
pkgVersionId:
values.pkgVersionId?.trim()?.length > 0
? values.pkgVersionId.trim()
: undefined,
});
setPkgVersion(res);
}}
>
<Form.Item name="pkgId" label="Pkg ID">
<Input placeholder="Pkg ID" />
</Form.Item>
<Form.Item name="version" label="Pkg version string (empty for latest)">
<Input placeholder="Pkg version string" />
</Form.Item>
<Form.Item name="pkgVersionId" label="PkgVersion ID">
<Input placeholder="PkgVersion ID" />
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Lookup
</Button>
</Form.Item>
</Form>
<Form>
{pkgVersion && (
<>
<p>
<code>PkgVersion ID {pkgVersion.id}</code>
</p>
<p>
<code>Version {pkgVersion.version}</code>
</p>
<React.Suspense fallback={<Spinner />}>
<div style={{ height: 500 }}>
<LazyFullCodeEditor
language="json"
ref={editorRef}
defaultValue={
pkgVersion.model
? JSON.stringify(pkgVersion.model, undefined, 2)
: ""
}
/>
</div>
</React.Suspense>
<Form.Item>
<Button
onClick={async () => {
const data = editorRef.current?.getValue();
if (data && JSON.parse(data)) {
const res = await nonAuthCtx.api.savePkgVersionAsAdmin({
pkgVersionId: pkgVersion.id,
data,
});
notification.success({
message: `PkgVersion saved`,
});
}
}}
>
Save
</Button>
</Form.Item>
</>
)}
</Form>
</div>
);
}

interface AdminActions {
onRefresh: () => void;
}
Expand Down Expand Up @@ -2397,7 +2486,8 @@ export default function AdminPage({ nonAuthCtx }: { nonAuthCtx: NonAuthCtx }) {
<CopilotFeedbackView />
<AppAuthMetrics />
<DownloadAppMeta />
<EditBundle />
<EditProjectRevBundle />
<EditPkgVersionBundle />
</div>
</AdminContext.Provider>
</NonAuthCtxContext.Provider>
Expand Down
10 changes: 10 additions & 0 deletions platform/wab/src/wab/server/AppServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,16 @@ export function addMainAppServerRoutes(app: express.Application) {
adminOnly,
withNext(adminRoutes.saveProjectRevisionData)
);
app.get(
`/api/v1/admin/pkg-version/data`,
adminOnly,
withNext(adminRoutes.getPkgVersion)
);
app.post(
`/api/v1/admin/pkg-version/:pkgVersionId`,
adminOnly,
withNext(adminRoutes.savePkgVersion)
);

/**
* Self routes
Expand Down
34 changes: 34 additions & 0 deletions platform/wab/src/wab/server/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ListInviteRequestsResponse,
ListUsersResponse,
LoginResponse,
PkgVersionId,
ProjectId,
RemoveWhitelistRequest,
TeamId,
Expand Down Expand Up @@ -357,6 +358,39 @@ export async function saveProjectRevisionData(req: Request, res: Response) {
});
}

export async function getPkgVersion(req: Request, res: Response) {
const mgr = superDbMgr(req);
let pkgVersion: PkgVersion;
if (req.query.pkgVersionId) {
pkgVersion = await mgr.getPkgVersionById(
req.query.pkgVersionId as PkgVersionId
);
} else if (req.query.pkgId) {
pkgVersion = await mgr.getPkgVersion(
req.query.pkgId as string,
req.query.version as string | undefined
);
} else {
throw new BadRequestError("Must specify either PkgVersion ID or Pkg ID");
}
res.json({
pkgVersion,
});
}

export async function savePkgVersion(req: Request, res: Response) {
const mgr = superDbMgr(req);
const pkgVersionId = req.params.pkgVersionId;
const data = req.body.data as string;
const pkgVersion = await mgr.getPkgVersionById(pkgVersionId);
await mgr.updatePkgVersion(pkgVersion.pkgId, pkgVersion.version, null, {
model: data,
});
res.json({
pkgVersion,
});
}

export async function deactivateUser(req: Request, res: Response) {
const mgr = superDbMgr(req);
const email = req.body.email;
Expand Down
29 changes: 29 additions & 0 deletions platform/wab/src/wab/shared/SharedApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,35 @@ export abstract class SharedApi {
return res.rev;
}

async getPkgVersionAsAdmin(opts: {
pkgId?: string;
version?: string;
pkgVersionId?: string;
}): Promise<PkgVersionInfo> {
const search = new URLSearchParams();
if (opts.pkgId) {
search.set("pkgId", opts.pkgId);
}
if (opts.version) {
search.set("version", opts.version);
}
if (opts.pkgVersionId) {
search.set("pkgVersionId", opts.pkgVersionId);
}
const res = await this.get(`/admin/pkg-version/data?${search.toString()}`);
return res.pkgVersion as PkgVersionInfo;
}

async savePkgVersionAsAdmin(opts: {
pkgVersionId: string;
data: string;
}): Promise<PkgVersionInfo> {
const res = await this.post(`/admin/pkg-version/${opts.pkgVersionId}`, {
data: opts.data,
});
return res.pkgVersion as PkgVersionInfo;
}

async saveProjectRevisionDataAsAdmin(
projectId: string,
revision: number,
Expand Down

0 comments on commit d402189

Please sign in to comment.