activities
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import PocketBase from "pocketbase";
|
||||
import { PlayerInfo } from "@/features/players/types";
|
||||
|
||||
export interface Activity {
|
||||
id: string;
|
||||
name: string;
|
||||
player?: string;
|
||||
player?: string | PlayerInfo;
|
||||
duration: number;
|
||||
success: boolean;
|
||||
error?: string;
|
||||
@@ -23,6 +24,23 @@ export interface ActivityInput {
|
||||
user_agent?: string;
|
||||
}
|
||||
|
||||
export interface ActivityListResult {
|
||||
items: Activity[];
|
||||
page: number;
|
||||
perPage: number;
|
||||
totalPages: number;
|
||||
totalItems: number;
|
||||
}
|
||||
|
||||
export interface ActivitySearchParams {
|
||||
page?: number;
|
||||
perPage?: number;
|
||||
name?: string;
|
||||
player?: string;
|
||||
success?: boolean;
|
||||
sortBy?: string;
|
||||
}
|
||||
|
||||
export function createActivitiesService(pb: PocketBase) {
|
||||
return {
|
||||
async createActivity(data: ActivityInput): Promise<Activity> {
|
||||
@@ -30,6 +48,47 @@ export function createActivitiesService(pb: PocketBase) {
|
||||
return result;
|
||||
},
|
||||
|
||||
async searchActivities(params: ActivitySearchParams = {}): Promise<ActivityListResult> {
|
||||
const {
|
||||
page = 1,
|
||||
perPage = 100,
|
||||
name,
|
||||
player,
|
||||
success,
|
||||
sortBy = "-created"
|
||||
} = params;
|
||||
|
||||
const filters: string[] = [];
|
||||
|
||||
if (name) {
|
||||
filters.push(`name ~ "${name}"`);
|
||||
}
|
||||
|
||||
if (player) {
|
||||
filters.push(`player = "${player}"`);
|
||||
}
|
||||
|
||||
if (success !== undefined) {
|
||||
filters.push(`success = ${success}`);
|
||||
}
|
||||
|
||||
const filterString = filters.length > 0 ? filters.join(" && ") : "";
|
||||
|
||||
const result = await pb.collection("activities").getList<Activity>(page, perPage, {
|
||||
filter: filterString,
|
||||
sort: sortBy,
|
||||
expand: "player",
|
||||
});
|
||||
|
||||
return {
|
||||
items: result.items,
|
||||
page: result.page,
|
||||
perPage: result.perPage,
|
||||
totalPages: result.totalPages,
|
||||
totalItems: result.totalItems,
|
||||
};
|
||||
},
|
||||
|
||||
async getRecentActivities(limit: number = 100): Promise<Activity[]> {
|
||||
const result = await pb.collection("activities").getList<Activity>(1, limit, {
|
||||
sort: "-created",
|
||||
@@ -39,7 +98,7 @@ export function createActivitiesService(pb: PocketBase) {
|
||||
|
||||
async getActivitiesByUser(userId: string, limit: number = 50): Promise<Activity[]> {
|
||||
const result = await pb.collection("activities").getList<Activity>(1, limit, {
|
||||
filter: `user_id = "${userId}"`,
|
||||
filter: `player = "${userId}"`,
|
||||
sort: "-created",
|
||||
});
|
||||
return result.items;
|
||||
@@ -47,7 +106,7 @@ export function createActivitiesService(pb: PocketBase) {
|
||||
|
||||
async getActivitiesByFunction(functionName: string, limit: number = 50): Promise<Activity[]> {
|
||||
const result = await pb.collection("activities").getList<Activity>(1, limit, {
|
||||
filter: `function_name = "${functionName}"`,
|
||||
filter: `name = "${functionName}"`,
|
||||
sort: "-created",
|
||||
});
|
||||
return result.items;
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Player, PlayerInfo } from "@/features/players/types";
|
||||
import { Team, TeamInfo } from "@/features/teams/types";
|
||||
import { Tournament, TournamentInfo } from "@/features/tournaments/types";
|
||||
import { Badge, BadgeInfo, BadgeProgress } from "@/features/badges/types";
|
||||
import { Activity } from "../services/activities";
|
||||
|
||||
// pocketbase does this weird thing with relations where it puts them under a seperate "expand" field
|
||||
// this file transforms raw pocketbase results to our types
|
||||
@@ -312,3 +313,18 @@ export function transformBadgeProgress(record: any): BadgeProgress {
|
||||
updated: record.updated,
|
||||
};
|
||||
}
|
||||
|
||||
export function transformActivity(record: any): Activity {
|
||||
return {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
player: record.expand?.player ? transformPlayerInfo(record.expand.player) : record.player,
|
||||
duration: record.duration,
|
||||
success: record.success,
|
||||
error: record.error,
|
||||
arguments: record.arguments,
|
||||
user_agent: record.user_agent,
|
||||
created: record.created,
|
||||
updated: record.updated,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user