session.secret wird ohne API-Key nicht zurückgegeben. Login nutzt daher session.userId und die Admin Users API statt account.get(). Co-authored-by: Cursor <cursoragent@cursor.com>
90 lines
2.2 KiB
JavaScript
90 lines
2.2 KiB
JavaScript
import { Client, Account, Databases, ID, Query, Users } from 'node-appwrite'
|
|
import { config } from '../config.js'
|
|
|
|
export function createAdminClient() {
|
|
const client = new Client()
|
|
.setEndpoint(config.appwrite.endpoint)
|
|
.setProject(config.appwrite.projectId)
|
|
.setKey(config.appwrite.apiKey)
|
|
|
|
return {
|
|
client,
|
|
databases: new Databases(client),
|
|
users: new Users(client),
|
|
}
|
|
}
|
|
|
|
export function createUserClient() {
|
|
const client = new Client()
|
|
.setEndpoint(config.appwrite.endpoint)
|
|
.setProject(config.appwrite.projectId)
|
|
|
|
return {
|
|
client,
|
|
account: new Account(client),
|
|
}
|
|
}
|
|
|
|
export async function listDocuments(collectionId, queries = []) {
|
|
const { databases } = createAdminClient()
|
|
const response = await databases.listDocuments(
|
|
config.appwrite.databaseId,
|
|
collectionId,
|
|
queries
|
|
)
|
|
return response.documents
|
|
}
|
|
|
|
export async function getCustomerByAppwriteUserId(appwriteUserId) {
|
|
const docs = await listDocuments(config.collections.customers, [
|
|
Query.equal('appwriteUserId', appwriteUserId),
|
|
Query.limit(1),
|
|
])
|
|
return docs[0] || null
|
|
}
|
|
|
|
export async function getPortalAccessByCustomerId(customerId) {
|
|
const docs = await listDocuments(config.collections.customerPortalAccess, [
|
|
Query.equal('customerId', customerId),
|
|
Query.limit(1),
|
|
])
|
|
return docs[0] || null
|
|
}
|
|
|
|
export async function updateDocument(collectionId, documentId, data) {
|
|
const { databases } = createAdminClient()
|
|
return databases.updateDocument(
|
|
config.appwrite.databaseId,
|
|
collectionId,
|
|
documentId,
|
|
data
|
|
)
|
|
}
|
|
|
|
export async function upsertWebsiteProjectByRepo(repoFullName, data) {
|
|
const { databases } = createAdminClient()
|
|
const existing = await listDocuments(config.collections.websiteProjects, [
|
|
Query.equal('repoFullName', repoFullName),
|
|
Query.limit(1),
|
|
])
|
|
|
|
const now = new Date().toISOString()
|
|
const payload = { ...data, updatedAt: now }
|
|
|
|
if (existing[0]) {
|
|
return databases.updateDocument(
|
|
config.appwrite.databaseId,
|
|
config.collections.websiteProjects,
|
|
existing[0].$id,
|
|
payload
|
|
)
|
|
}
|
|
|
|
return databases.createDocument(
|
|
config.appwrite.databaseId,
|
|
config.collections.websiteProjects,
|
|
ID.unique(),
|
|
{ ...payload, createdAt: now }
|
|
)
|
|
}
|