diff --git a/core/actions/corePubFields.ts b/core/actions/corePubFields.ts deleted file mode 100644 index e86bce6cdd..0000000000 --- a/core/actions/corePubFields.ts +++ /dev/null @@ -1,112 +0,0 @@ -import type { JSONSchemaType } from "ajv"; - -import { CoreSchemaType } from "db/public"; - -export type BasePubField = { - id?: string; - name: string; - slug: string; - schemaName?: CoreSchemaType; - schema: { - name: string; - namespace: Namespace; - schema: JSONSchemaType; - }; -}; - -export type CorePubField = BasePubField<"pubpub">; - -export const title = { - name: "Title", - slug: "pubpub:title", - schemaName: CoreSchemaType.String, - schema: { - name: "title", - namespace: "pubpub", - schema: { - $id: "pubpub:title", - title: "Title", - type: "string", - } satisfies JSONSchemaType, - }, -} as const satisfies CorePubField; - -export const content = { - name: "Content", - slug: "pubpub:content", - schemaName: CoreSchemaType.String, - schema: { - name: "content", - namespace: "pubpub", - schema: { - $id: "pubpub:content", - title: "Content", - type: "string", - } satisfies JSONSchemaType, - }, -} as const satisfies CorePubField; - -export const v6PubId = { - name: "V6 Pub ID", - slug: "pubpub:v6-pub-id", - schemaName: CoreSchemaType.String, - schema: { - name: "v6-pub-id", - namespace: "pubpub", - schema: { - $id: "pubpub:v6-pub-id", - title: "V6 Pub ID", - type: "string", - } satisfies JSONSchemaType, - }, -} as const satisfies CorePubField; - -// these are just to play around with the pubfields in actions for now -export const email = { - name: "Email", - slug: "pubpub:email", - schemaName: CoreSchemaType.Email, - schema: { - name: "email", - namespace: "pubpub", - schema: { - $id: "pubpub:email", - title: "Email", - format: "email", - type: "string", - } satisfies JSONSchemaType, - }, -} as const satisfies CorePubField; - -export const url = { - name: "URL", - slug: "pubpub:url", - schemaName: CoreSchemaType.URL, - schema: { - name: "url", - namespace: "pubpub", - schema: { - $id: "pubpub:url", - title: "URL", - format: "url", - type: "string", - } satisfies JSONSchemaType, - }, -} as const satisfies CorePubField; - -export const userId = { - name: "User ID", - slug: "pubpub:user-id", - schemaName: CoreSchemaType.MemberId, - schema: { - name: "userId", - namespace: "pubpub", - schema: { - $id: "pubpub:user-id", - title: "User ID", - type: "string", - } satisfies JSONSchemaType, - }, -} as const satisfies CorePubField; - -export const corePubFields = [title, content, v6PubId, email, url, userId] as const; diff --git a/core/actions/pushToV6/action.ts b/core/actions/pushToV6/action.ts index cd3043ccb3..2b0f421f70 100644 --- a/core/actions/pushToV6/action.ts +++ b/core/actions/pushToV6/action.ts @@ -13,6 +13,10 @@ export const action = defineAction({ authToken: z.string().describe("PubPub v6 API auth token"), title: z.string().describe("Title of the Pub"), content: z.string().describe("Content of the Pub"), + idField: z + .string() + .regex(/\w+:\w+/) + .describe("Field on this pub to write to id to|ID Field"), }), }, description: "Sync a PubPub Platform pub to v6", diff --git a/core/actions/pushToV6/run.ts b/core/actions/pushToV6/run.ts index 0f5ca27096..fe2bd85fe3 100644 --- a/core/actions/pushToV6/run.ts +++ b/core/actions/pushToV6/run.ts @@ -11,7 +11,6 @@ import type { ClientExceptionOptions } from "~/lib/serverActions"; import { db } from "~/kysely/database"; import { autoRevalidate } from "~/lib/server/cache/autoRevalidate"; import { isClientExceptionOptions } from "~/lib/serverActions"; -import * as corePubFields from "../corePubFields"; import { defineRun } from "../types"; const authError = { @@ -159,14 +158,16 @@ const updateV6PubText = async ( } }; -const updateV6PubId = async (pubId: PubsId, v6PubId: string, lastModifiedBy: LastModifiedBy) => { +const updateV6PubId = async ( + pubId: PubsId, + v6PubId: string, + lastModifiedBy: LastModifiedBy, + idField: string +) => { await autoRevalidate( db .with("field", (db) => - db - .selectFrom("pub_fields") - .select("id") - .where("slug", "=", corePubFields.v6PubId.slug) + db.selectFrom("pub_fields").select("id").where("slug", "=", idField) ) .insertInto("pub_values") .values((eb) => ({ @@ -188,7 +189,7 @@ export const run = defineRun(async ({ pub, config, args, lastModi let v6Pub: { id: string }; - const v6PubId = pub.values.find((value) => value.fieldSlug === corePubFields.v6PubId.slug) + const v6PubId = pub.values.find((value) => value.fieldSlug === config.idField) ?.value as string; // Fetch the pub if the v7 pub already had a v6 pub id if (v6PubId) { @@ -220,7 +221,7 @@ export const run = defineRun(async ({ pub, config, args, lastModi v6Pub = createV6PubResponse; // Update the v6 pub id in the v7 pub - await updateV6PubId(pub.id, v6Pub.id, lastModifiedBy); + await updateV6PubId(pub.id, v6Pub.id, lastModifiedBy, config.idField); } // Update the v6 pub content diff --git a/core/actions/types.ts b/core/actions/types.ts index 0ec212b517..53943a3ba1 100644 --- a/core/actions/types.ts +++ b/core/actions/types.ts @@ -14,11 +14,8 @@ import type { Dependency, FieldConfig, FieldConfigItem } from "ui/auto-form"; import type * as Icons from "ui/icon"; import { Event } from "db/public"; -import type { CorePubField } from "./corePubFields"; import type { ClientExceptionOptions } from "~/lib/serverActions"; -export type ActionPubType = CorePubField[]; - type ZodObjectOrWrapped = z.ZodObject | z.ZodEffects>; export type ZodObjectOrWrappedOrOptional = ZodObjectOrWrapped | z.ZodOptional; @@ -28,11 +25,7 @@ export type ActionPub = ProcessedPub<{ }>; export type RunProps = - T extends Action< - infer P extends ActionPubType, - infer C, - infer A extends ZodObjectOrWrappedOrOptional - > + T extends Action ? { config: C["_output"] & { pubFields: { [K in keyof C["_output"]]?: string[] } }; configFieldOverrides: Set; @@ -67,7 +60,6 @@ export type TokenDef = { }; export type Action< - P extends ActionPubType = ActionPubType, C extends ZodObjectOrWrapped = ZodObjectOrWrapped, A extends ZodObjectOrWrappedOrOptional = ZodObjectOrWrappedOrOptional, N extends ActionName = ActionName, @@ -139,12 +131,11 @@ export type Action< }; export const defineAction = < - T extends ActionPubType, C extends ZodObjectOrWrapped, A extends ZodObjectOrWrappedOrOptional, N extends ActionName, >( - action: Action + action: Action ) => action; export type ActionSuccess = {