forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps.ts
More file actions
136 lines (125 loc) · 3.57 KB
/
Copy pathapps.ts
File metadata and controls
136 lines (125 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import omiApiClient, { adminInit } from "./client";
import {
delete_app_v1_apps__app_id__delete,
get_app_details_v1_apps__app_id__get,
get_apps_v1_apps_get,
get_unapproved_public_apps_v1_apps_public_unapproved_get,
type UnapprovedPublicAppResponse,
} from "./omiApi.generated";
import type { OmiApp, OmiAppInput } from "./types";
import { getDb } from "@/lib/firebase/admin";
const APPS_ENDPOINT = "/v1/apps";
/**
* Fetches a list of all apps.
* @param uid - The UID of the authenticated user.
*/
export async function getApps(uid: string): Promise<OmiApp[]> {
return get_apps_v1_apps_get({}, {}, adminInit(uid));
}
/**
* Fetches a single app by its ID.
* @param uid - The UID of the authenticated user.
*/
export async function getAppById(uid: string, appId: string): Promise<OmiApp> {
return get_app_details_v1_apps__app_id__get(
{ app_id: appId },
{},
adminInit(uid),
);
}
/**
* Creates a new app.
* @param uid - The UID of the authenticated user.
*/
export async function createApp(
uid: string,
appData: OmiAppInput,
): Promise<OmiApp> {
// Not wired: POST /v1/apps needs multipart form+file; generated client omits body.
return await omiApiClient<OmiApp>(APPS_ENDPOINT, uid, {
method: "POST",
body: appData,
});
}
/**
* Updates an existing app.
* @param uid - The UID of the authenticated user.
*/
export async function updateApp(
uid: string,
appId: string,
appData: Partial<OmiAppInput>,
): Promise<OmiApp> {
// Not wired: PATCH /v1/apps/{id} needs multipart form; generated client omits body.
const endpoint = `${APPS_ENDPOINT}/${appId}`;
return await omiApiClient<OmiApp>(endpoint, uid, {
method: "PATCH",
body: appData,
});
}
/**
* Deletes an app by its ID.
* @param uid - The UID of the authenticated user.
*/
export async function deleteApp(uid: string, appId: string): Promise<void> {
await delete_app_v1_apps__app_id__delete({ app_id: appId }, {}, adminInit(uid));
}
// Fetch all public unapproved apps
export const getUnapprovedApps = async (
uid: string,
): Promise<UnapprovedPublicAppResponse[]> => {
const secretKey = process.env.OMI_API_SECRET_KEY;
if (!secretKey) {
throw new Error("OMI_API_SECRET_KEY environment variable is not set.");
}
return get_unapproved_public_apps_v1_apps_public_unapproved_get(
{ secret_key: secretKey },
adminInit(uid),
);
};
/**
* Submits a review action (approve/reject) for an app.
* @param uid - The UID of the authenticated admin user.
* @param appId - The ID of the app to review.
* @param action - The review action ('approve' or 'reject').
* @param reason - Optional reason for rejection.
*/
export async function reviewApp(
uid: string,
appId: string,
action: "approve" | "reject",
reason?: string,
): Promise<{ success: boolean; message?: string }> {
const db = getDb();
const appDocRef = db.collection("plugins_data").doc(appId);
let dataToUpdate: {
status: string;
approved: boolean;
};
if (action === "approve") {
dataToUpdate = {
status: "approved",
approved: true,
};
} else {
// action === 'reject'
dataToUpdate = {
status: "rejected",
approved: false,
};
}
try {
await appDocRef.update(dataToUpdate);
console.log(`App ${appId} status updated to ${action} by user ${uid}.`); // Optional: for server logs
return { success: true };
} catch (error: any) {
console.error(
`Error updating app ${appId} to ${action} by user ${uid}:`,
error,
);
return {
success: false,
message: error.message || `Failed to ${action} app ${appId}.`,
};
}
}