@adventurelabs/scout-core 1.4.79 → 1.4.80

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.
@@ -31,6 +31,7 @@ export * from "./versions_software";
31
31
  export * from "./versions_software_server";
32
32
  export * from "./artifacts";
33
33
  export * from "./pins";
34
+ export * from "./points_for_user";
34
35
  export * from "./pubsub_token";
35
36
  export * from "./pubsub_token_server";
36
37
  export * from "./client_abilities_token";
@@ -31,6 +31,7 @@ export * from "./versions_software";
31
31
  export * from "./versions_software_server";
32
32
  export * from "./artifacts";
33
33
  export * from "./pins";
34
+ export * from "./points_for_user";
34
35
  export * from "./pubsub_token";
35
36
  export * from "./pubsub_token_server";
36
37
  export * from "./client_abilities_token";
@@ -0,0 +1,11 @@
1
+ import { SupabaseClient } from "@supabase/supabase-js";
2
+ import { Database } from "../types/supabase";
3
+ import { IPointsForUser } from "../types/db";
4
+ import { IWebResponseCompatible } from "../types/requests";
5
+ export type PointsBalanceUpdateResult = {
6
+ balance: number;
7
+ entry: IPointsForUser | null;
8
+ };
9
+ export declare function get_points_for_user_by_user_id(client: SupabaseClient<Database>, user_id: string): Promise<IWebResponseCompatible<IPointsForUser[]>>;
10
+ export declare function get_points_balance_for_user_id(client: SupabaseClient<Database>, user_id: string): Promise<IWebResponseCompatible<number>>;
11
+ export declare function update_points_balance_for_user(client: SupabaseClient<Database>, user_id: string, balance: number, update_reason: string): Promise<IWebResponseCompatible<PointsBalanceUpdateResult | null>>;
@@ -0,0 +1,54 @@
1
+ import { EnumWebResponse, IWebResponse } from "../types/requests";
2
+ async function sum_points_for_user(client, user_id) {
3
+ const { data, error } = await client
4
+ .from("points_for_user")
5
+ .select("points")
6
+ .eq("user_id", user_id);
7
+ if (error) {
8
+ return IWebResponse.error(error.message).to_compatible();
9
+ }
10
+ const balance = (data ?? []).reduce((sum, row) => sum + row.points, 0);
11
+ return IWebResponse.success(balance).to_compatible();
12
+ }
13
+ export async function get_points_for_user_by_user_id(client, user_id) {
14
+ const { data, error } = await client
15
+ .from("points_for_user")
16
+ .select("*")
17
+ .eq("user_id", user_id)
18
+ .order("inserted_at", { ascending: false });
19
+ if (error) {
20
+ return IWebResponse.error(error.message).to_compatible();
21
+ }
22
+ return IWebResponse.success(data ?? []).to_compatible();
23
+ }
24
+ export async function get_points_balance_for_user_id(client, user_id) {
25
+ return sum_points_for_user(client, user_id);
26
+ }
27
+ export async function update_points_balance_for_user(client, user_id, balance, update_reason) {
28
+ if (!user_id) {
29
+ return IWebResponse.error("user_id is required").to_compatible();
30
+ }
31
+ if (!update_reason.trim()) {
32
+ return IWebResponse.error("update_reason is required").to_compatible();
33
+ }
34
+ const current = await sum_points_for_user(client, user_id);
35
+ if (current.status !== EnumWebResponse.SUCCESS || current.data == null) {
36
+ return IWebResponse.error(current.msg ?? "Failed to load points balance").to_compatible();
37
+ }
38
+ const delta = balance - current.data;
39
+ if (delta === 0) {
40
+ return IWebResponse.success({ balance: current.data, entry: null }).to_compatible();
41
+ }
42
+ const { data, error } = await client
43
+ .from("points_for_user")
44
+ .insert([{ user_id, points: delta, update_reason: update_reason.trim() }])
45
+ .select("*")
46
+ .single();
47
+ if (error) {
48
+ return IWebResponse.error(error.message).to_compatible();
49
+ }
50
+ if (!data) {
51
+ return IWebResponse.error("Failed to update points balance").to_compatible();
52
+ }
53
+ return IWebResponse.success({ balance, entry: data }).to_compatible();
54
+ }
package/dist/index.d.ts CHANGED
@@ -30,6 +30,7 @@ export * from "./helpers/gps";
30
30
  export * from "./helpers/herds";
31
31
  export * from "./helpers/location";
32
32
  export * from "./helpers/pins";
33
+ export * from "./helpers/points_for_user";
33
34
  export * from "./helpers/plans";
34
35
  export * from "./helpers/layers";
35
36
  export * from "./helpers/sessions";
@@ -82,5 +83,5 @@ export * from "./supabase/middleware";
82
83
  export * from "./supabase/server";
83
84
  export * from "./api_keys/actions";
84
85
  export type { HerdModule, IHerdModule } from "./types/herd_module";
85
- export type { IDevice, IEvent, IUser, IHerd, IHerdPrettyLocation, IEventWithTags, IZoneWithActions, ICredential, CredentialInsert, CredentialUpdate, ICertificate, CertificateInsert, CertificateUpdate, IUserAndRole, IUserProfileRow, IHerdInvitation, IHerdInvitationWithHerdSlug, IHerdAllowedDomain, IUserRolePerHerd, IAbility, IHerdRole, IHerdRoleWithAbilities, HerdRoleSystemName, HerdInvitationStatus, IApiKeyScout, ILayer, IHeartbeat, IProvider, IConnectivity, ISession, ISessionWithCoordinates, IConnectivityWithCoordinates, IObservation, ObservationInsert, ObservationUpdate, IAnalysisJob, IAnalysisTask, AnalysisWorkStatus, } from "./types/db";
86
+ export type { IDevice, IEvent, IUser, IHerd, IHerdPrettyLocation, IEventWithTags, IZoneWithActions, ICredential, CredentialInsert, CredentialUpdate, IPointsForUser, PointsForUserInsert, PointsForUserUpdate, ICertificate, CertificateInsert, CertificateUpdate, IUserAndRole, IUserProfileRow, IHerdInvitation, IHerdInvitationWithHerdSlug, IHerdAllowedDomain, IUserRolePerHerd, IAbility, IHerdRole, IHerdRoleWithAbilities, HerdRoleSystemName, HerdInvitationStatus, IApiKeyScout, ILayer, IHeartbeat, IProvider, IConnectivity, ISession, ISessionWithCoordinates, IConnectivityWithCoordinates, IObservation, ObservationInsert, ObservationUpdate, IAnalysisJob, IAnalysisTask, AnalysisWorkStatus, } from "./types/db";
86
87
  export { EnumSessionsVisibility } from "./types/events";
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ export * from "./helpers/gps";
33
33
  export * from "./helpers/herds";
34
34
  export * from "./helpers/location";
35
35
  export * from "./helpers/pins";
36
+ export * from "./helpers/points_for_user";
36
37
  export * from "./helpers/plans";
37
38
  export * from "./helpers/layers";
38
39
  export * from "./helpers/sessions";
@@ -48,6 +48,7 @@ export type IObservation = Database["public"]["Tables"]["observations"]["Row"];
48
48
  export type IProvider = Database["public"]["Tables"]["providers"]["Row"];
49
49
  export type IPart = Database["public"]["Tables"]["parts"]["Row"];
50
50
  export type ICredential = Database["public"]["Tables"]["credentials"]["Row"];
51
+ export type IPointsForUser = Database["public"]["Tables"]["points_for_user"]["Row"];
51
52
  export type ICertificate = Database["public"]["Tables"]["certificates"]["Row"];
52
53
  export type IVersionsSoftware = Database["public"]["Tables"]["versions_software"]["Row"];
53
54
  export type IArtifact = Database["public"]["Tables"]["artifacts"]["Row"];
@@ -90,6 +91,8 @@ export type ISessionUsageOverTime = Database["public"]["Functions"]["get_session
90
91
  export type PartInsert = Database["public"]["Tables"]["parts"]["Insert"];
91
92
  export type CredentialInsert = Database["public"]["Tables"]["credentials"]["Insert"];
92
93
  export type CredentialUpdate = Database["public"]["Tables"]["credentials"]["Update"];
94
+ export type PointsForUserInsert = Database["public"]["Tables"]["points_for_user"]["Insert"];
95
+ export type PointsForUserUpdate = Database["public"]["Tables"]["points_for_user"]["Update"];
93
96
  export type CertificateInsert = Database["public"]["Tables"]["certificates"]["Insert"];
94
97
  export type CertificateUpdate = Database["public"]["Tables"]["certificates"]["Update"];
95
98
  export type VersionsSoftwareInsert = Database["public"]["Tables"]["versions_software"]["Insert"];
@@ -1375,6 +1375,38 @@ export type Database = {
1375
1375
  }
1376
1376
  ];
1377
1377
  };
1378
+ points_for_user: {
1379
+ Row: {
1380
+ id: number;
1381
+ inserted_at: string;
1382
+ points: number;
1383
+ update_reason: string;
1384
+ user_id: string;
1385
+ };
1386
+ Insert: {
1387
+ id?: number;
1388
+ inserted_at?: string;
1389
+ points: number;
1390
+ update_reason: string;
1391
+ user_id: string;
1392
+ };
1393
+ Update: {
1394
+ id?: number;
1395
+ inserted_at?: string;
1396
+ points?: number;
1397
+ update_reason?: string;
1398
+ user_id?: string;
1399
+ };
1400
+ Relationships: [
1401
+ {
1402
+ foreignKeyName: "points_for_user_user_id_fkey";
1403
+ columns: ["user_id"];
1404
+ isOneToOne: false;
1405
+ referencedRelation: "users";
1406
+ referencedColumns: ["id"];
1407
+ }
1408
+ ];
1409
+ };
1378
1410
  providers: {
1379
1411
  Row: {
1380
1412
  created_at: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adventurelabs/scout-core",
3
- "version": "1.4.79",
3
+ "version": "1.4.80",
4
4
  "description": "Core utilities and helpers for Adventure Labs Scout applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",