File size: 6,054 Bytes
3eedfc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as sdk from 'node-appwrite';
import { createLogger } from '../utils/logger.js';
import dotenv from 'dotenv';

dotenv.config();

const logger = createLogger('AppwriteService');

class AppwriteService {
    constructor() {
        this.config = {
            endpoint: process.env.APPWRITE_ENDPOINT || 'https://cloud.appwrite.io/v1',
            projectId: process.env.APPWRITE_PROJECT_ID,
            apiKey: process.env.APPWRITE_API_KEY,
            functionId: process.env.APPWRITE_FUNCTION_ID_PRS || '68ffcb25003df2ce3663', // Default to PRS function ID
        };

        this._validateConfig();
        this._initializeClient();
    }

    _validateConfig() {
        if (!this.config.projectId || !this.config.apiKey) {
            logger.warn('INIT', 'Appwrite credentials missing. Service will be disabled.');
            this.enabled = false;
        } else {
            this.enabled = true;
        }
    }

    _initializeClient() {
        if (!this.enabled) return;

        try {
            this.client = new sdk.Client()
                .setEndpoint(this.config.endpoint)
                .setProject(this.config.projectId)
                .setKey(this.config.apiKey);

            this.functions = new sdk.Functions(this.client);
            logger.info('INIT', 'Appwrite SDK initialized');
        } catch (error) {
            logger.error('INIT', 'Failed to initialize Appwrite SDK', error);
            this.enabled = false;
        }
    }

    /**
     * Execute an Appwrite Cloud Function
     * @param {string} functionId - The ID of the function to execute (optional, defaults to config)
     * @param {Object} payload - The data to send to the function
     * @param {string} requestId - Request ID for logging
     * @returns {Promise<Object>} - The parsed JSON response from the function
     */
    async executeFunction(payload, requestId = 'default', functionId = null) {
        if (!this.enabled) {
            throw new Error('Appwrite service is not configured');
        }

        const targetFunctionId = functionId || this.config.functionId;
        const startTime = Date.now();

        try {
            logger.debug(requestId, 'Executing Appwrite function', { functionId: targetFunctionId });

            const execution = await this.functions.createExecution(
                targetFunctionId,
                JSON.stringify(payload),
                false, // async (false = synchronous)
                '/',   // path
                'POST', // method
                {}     // headers
            );

            const duration = Date.now() - startTime;

            if (execution.status === 'failed') {
                logger.error(requestId, 'Appwrite execution failed', {
                    stderr: execution.stderr,
                    duration
                });
                throw new Error(`Appwrite function execution failed: ${execution.stderr}`);
            }

            let responseData;
            try {
                responseData = JSON.parse(execution.responseBody);

                // DEBUG LOGGING
                let logData = { ...responseData };
                if (logData.data) {
                    logData.data = { ...logData.data };
                    if (logData.data.attendanceTable) logData.data.attendanceTable = `[HIDDEN - ${logData.data.attendanceTable.length} items]`;
                    if (logData.data.debatesTable) logData.data.debatesTable = `[HIDDEN - ${logData.data.debatesTable.length} items]`;
                    if (logData.data.questionsTable) logData.data.questionsTable = `[HIDDEN - ${logData.data.questionsTable.length} items]`;
                }

                console.log('\n======================================================================');
                console.log(`🚀 APPWRITE RESPONSE [${requestId}]`);
                console.log('======================================================================');
                console.log(JSON.stringify(logData, null, 2));
                console.log('======================================================================\n');

            } catch (parseError) {
                logger.error(requestId, 'Failed to parse response body', {
                    body: execution.responseBody,
                    error: parseError.message
                });

                // Log raw body on error
                console.log('\n======================================================================');
                console.log(`❌ APPWRITE RESPONSE ERROR [${requestId}]`);
                console.log('======================================================================');
                console.log(execution.responseBody);
                console.log('======================================================================\n');

                throw new Error('Invalid JSON response from Appwrite function');
            }

            logger.success(requestId, `Appwrite execution completed in ${duration}ms`, {
                status: execution.status,
                success: responseData.success
            });

            return {
                success: true,
                data: responseData,
                execution: {
                    id: execution.$id,
                    status: execution.status,
                    duration: execution.duration,
                },
                timing: {
                    total: duration
                }
            };

        } catch (error) {
            const duration = Date.now() - startTime;
            logger.error(requestId, 'Appwrite execution error', { error: error.message, duration });
            throw error;
        }
    }

    getStats() {
        return {
            enabled: this.enabled,
            config: {
                endpoint: this.config.endpoint,
                projectId: this.config.projectId ? '***' : 'missing',
                functionId: this.config.functionId
            }
        };
    }
}

// Export singleton
export default new AppwriteService();