@pylonsync/create-pylon 0.22.69 → 0.22.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,12 @@
1
1
  import { query, v } from "@pylonsync/functions";
2
2
 
3
- // Internal: read a Generation by id, bypassing the owner read-policy (the
4
- // pollGeneration job runs without a user session). Returns the fields the job
5
- // needs, or null.
3
+ // Internal: read a Generation by id. pollGeneration checks the returned owner
4
+ // before it changes the row or calls the provider.
6
5
  export default query<
7
6
  { id: string },
8
7
  | {
9
8
  id: string;
9
+ userId: string;
10
10
  kind: string;
11
11
  prompt: string;
12
12
  status: string;
@@ -18,7 +18,7 @@ export default query<
18
18
  args: { id: v.id("Generation") },
19
19
  async handler(ctx, args) {
20
20
  const g = (await ctx.db.unsafe.get("Generation", args.id)) as
21
- | { id: string; kind: string; prompt: string; status: string; predictionId?: string | null }
21
+ | { id: string; userId: string; kind: string; prompt: string; status: string; predictionId?: string | null }
22
22
  | null;
23
23
  return g;
24
24
  },
@@ -12,26 +12,31 @@ import { placeholderImage } from "../lib/studio";
12
12
  // With no REPLICATE_API_TOKEN it returns a clearly-labeled placeholder instead,
13
13
  // so the whole flow (and this job) work with zero config.
14
14
  //
15
- // Not internal, but harmless if called directly: it only advances a generation's
16
- // own prediction; results sync only to the owner (Generation is owner-read).
15
+ // Scheduled jobs retain the user identity from generate. Direct calls must
16
+ // have the same owner before they can update or schedule a generation.
17
17
 
18
18
  const REPLICATE = "https://api.replicate.com/v1";
19
19
  const POLL_MS = 3000;
20
20
  const MAX_ATTEMPTS = 200; // ~10 min ceiling, then give up
21
21
 
22
22
  export default action<{ id: string; attempts?: number }, { ok: boolean }>({
23
- auth: "public",
23
+ auth: "user",
24
24
  args: { id: v.id("Generation"), attempts: v.optional(v.int()) },
25
25
  async handler(ctx, args) {
26
+ const userId = ctx.auth.userId;
27
+ if (!userId) throw ctx.error("AUTH_REQUIRED", "Sign in to check a generation.");
26
28
  const attempts = args.attempts ?? 0;
27
29
  const gen = await ctx.runQuery<{
28
30
  id: string;
31
+ userId: string;
29
32
  kind: string;
30
33
  prompt: string;
31
34
  status: string;
32
35
  predictionId?: string | null;
33
36
  } | null>("_getGeneration", { id: args.id });
34
- if (!gen || gen.status === "done" || gen.status === "failed") return { ok: true };
37
+ if (!gen) return { ok: true };
38
+ if (gen.userId !== userId) throw ctx.error("NOT_FOUND", "Generation not found.");
39
+ if (gen.status === "done" || gen.status === "failed") return { ok: true };
35
40
 
36
41
  const update = (patch: Record<string, unknown>) => ctx.runMutation("_updateGeneration", { id: args.id, ...patch });
37
42
  const reschedule = () =>
@@ -1,14 +1,12 @@
1
1
  import { mutation, v } from "@pylonsync/functions";
2
2
 
3
- /**
4
- * Soft-delete: set archived = true. Project policy gates updates to
5
- * org owner/admin only, so a regular member can't archive a project
6
- * even if they created it. Restore by writing archived = false (this
7
- * scaffold doesn't ship a restore action — left as an exercise).
8
- */
3
+ /** Archive a project after checking organization administration access. */
9
4
  export default mutation({
10
5
  args: { id: v.id("Project") },
11
6
  async handler(ctx, args: { id: string }) {
7
+ const project = await ctx.db.get("Project", args.id);
8
+ if (!project) throw ctx.error("NOT_FOUND", "project not found");
9
+ await ctx.requireMember(project.orgId as string, { entity: "Membership", role: ["owner", "admin"] });
12
10
  await ctx.db.update("Project", args.id, { archived: true });
13
11
  return await ctx.db.get("Project", args.id);
14
12
  },
@@ -32,7 +32,8 @@ export default mutation({
32
32
  ownerId: ctx.auth.userId,
33
33
  createdAt: new Date().toISOString(),
34
34
  });
35
- await ctx.db.insert("Membership", {
35
+ // The caller just created this organization. No membership exists yet.
36
+ await ctx.db.unsafe.insert("Membership", {
36
37
  orgId,
37
38
  userId: ctx.auth.userId,
38
39
  role: "owner",
@@ -1,16 +1,13 @@
1
1
  import { mutation, v } from "@pylonsync/functions";
2
2
 
3
- /**
4
- * Create a Project inside an Org. Project policy already gates this
5
- * to org members; we additionally trim the name and stamp the caller
6
- * as creator.
7
- */
3
+ /** Create a project for an organization member. Use the caller as creator. */
8
4
  export default mutation({
9
5
  args: { orgId: v.id("Org"), name: v.string() },
10
6
  async handler(ctx, args: { orgId: string; name: string }) {
11
7
  if (!ctx.auth.userId) {
12
8
  throw ctx.error("UNAUTHENTICATED", "log in first");
13
9
  }
10
+ await ctx.requireMember(args.orgId, { entity: "Membership" });
14
11
  const name = args.name.trim();
15
12
  if (!name) throw ctx.error("EMPTY_NAME", "project name cannot be empty");
16
13
  const id = await ctx.db.insert("Project", {
@@ -1,15 +1,6 @@
1
1
  import { mutation, v } from "@pylonsync/functions";
2
2
 
3
- /**
4
- * Add a User to an Org with a role. Membership policy already gates
5
- * this to owners / admins; we additionally validate the role string
6
- * here (Pylon enforces enum at the schema level too, but a clearer
7
- * error message is nicer than a generic constraint failure).
8
- *
9
- * For a real app, replace this with an email-invite flow that emits
10
- * an Invite row + emails the user — but the wiring is the same: the
11
- * Membership row is what grants access.
12
- */
3
+ /** Add a member after checking the caller is an organization administrator. */
13
4
  const ROLES = new Set(["owner", "admin", "member"]);
14
5
 
15
6
  export default mutation({
@@ -28,6 +19,7 @@ export default mutation({
28
19
  `role must be one of ${[...ROLES].join(", ")}`,
29
20
  );
30
21
  }
22
+ await ctx.requireMember(args.orgId, { entity: "Membership", role: ["owner", "admin"] });
31
23
  const dup = (await ctx.db.query("Membership", {
32
24
  orgId: args.orgId,
33
25
  userId: args.userId,
@@ -1,13 +1,13 @@
1
1
  import { query, v } from "@pylonsync/functions";
2
2
 
3
- /**
4
- * List every member of an Org. Membership policy gates this to
5
- * org-admins + owners (members see only their own row), so non-admin
6
- * callers get a single-element array; admins get everyone.
7
- */
3
+ /** Administrators can list all members. Other members can read their own row. */
8
4
  export default query({
9
5
  args: { orgId: v.id("Org") },
10
6
  async handler(ctx, args: { orgId: string }) {
11
- return await ctx.db.query("Membership", { orgId: args.orgId });
7
+ const member = await ctx.requireMember(args.orgId, { entity: "Membership" });
8
+ const filter = member.role === "owner" || member.role === "admin"
9
+ ? { orgId: args.orgId }
10
+ : { orgId: args.orgId, userId: ctx.auth.userId };
11
+ return await ctx.db.query("Membership", filter);
12
12
  },
13
13
  });
@@ -1,13 +1,10 @@
1
1
  import { query, v } from "@pylonsync/functions";
2
2
 
3
- /**
4
- * Every Project in an Org. Project policy enforces tenant scope, so
5
- * even if the caller passes a foreign `orgId` they're not a member of,
6
- * the query returns nothing.
7
- */
3
+ /** List active projects after checking organization membership. */
8
4
  export default query({
9
5
  args: { orgId: v.id("Org") },
10
6
  async handler(ctx, args: { orgId: string }) {
7
+ await ctx.requireMember(args.orgId, { entity: "Membership" });
11
8
  const rows = (await ctx.db.query("Project", {
12
9
  orgId: args.orgId,
13
10
  })) as any[];
@@ -1,14 +1,10 @@
1
1
  import { mutation, v } from "@pylonsync/functions";
2
2
 
3
- /**
4
- * Remove a User from an Org. Refuses to remove the org's owner
5
- * (transfer ownership first via setMemberRole + a manual Org.ownerId
6
- * update — left as an exercise; this scaffold stops at the safety
7
- * check).
8
- */
3
+ /** Remove a member. Keep the founding owner in the organization. */
9
4
  export default mutation({
10
5
  args: { orgId: v.id("Org"), userId: v.id("User") },
11
6
  async handler(ctx, args: { orgId: string; userId: string }) {
7
+ await ctx.requireMember(args.orgId, { entity: "Membership", role: ["owner", "admin"] });
12
8
  const org = (await ctx.db.get("Org", args.orgId)) as any;
13
9
  if (!org) throw ctx.error("NOT_FOUND", "org not found");
14
10
  if (org.ownerId === args.userId) {
@@ -2,13 +2,7 @@ import { mutation, v } from "@pylonsync/functions";
2
2
 
3
3
  const ROLES = new Set(["owner", "admin", "member"]);
4
4
 
5
- /**
6
- * Promote/demote a member. Only one row per (orgId, userId) so we
7
- * find by composite then update. Setting role=owner does NOT change
8
- * Org.ownerId — those are separate concepts (the founder vs current
9
- * org-leadership members); this scaffold leaves transfer-of-ownership
10
- * as a separate flow.
11
- */
5
+ /** Change a membership role after checking organization administration access. */
12
6
  export default mutation({
13
7
  args: {
14
8
  orgId: v.id("Org"),
@@ -25,6 +19,7 @@ export default mutation({
25
19
  `role must be one of ${[...ROLES].join(", ")}`,
26
20
  );
27
21
  }
22
+ await ctx.requireMember(args.orgId, { entity: "Membership", role: ["owner", "admin"] });
28
23
  const memberships = (await ctx.db.query("Membership", {
29
24
  orgId: args.orgId,
30
25
  userId: args.userId,