File size: 7,968 Bytes
c055c3e
 
 
 
 
06fe38b
 
c055c3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3af90a1
 
 
 
c055c3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f30cee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c055c3e
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const { google } = require('googleapis');
const path = require('path');

class SheetsModule {
    constructor() {
        // Fallback to the ID explicitly provided by the user if the env variable isn't attached to Hugging Face Secrets yet
        this.sheetId = process.env.GOOGLE_SHEETS_ID || '1dY59KqOVfvP5-SnGOELfWY-4oyOWPHR6wIsuJ_0_W50';
        this.authJson = JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT_JSON || '{}');
    }

    async getClient() {
        if (!this.authJson.client_email) return null;
        
        const auth = new google.auth.GoogleAuth({
            credentials: this.authJson,
            scopes: ['https://www.googleapis.com/auth/spreadsheets']
        });
        return await auth.getClient();
    }

    async initializeSheet() {
        if (!this.sheetId) return { success: false, error: 'GOOGLE_SHEETS_ID not set' };

        try {
            const client = await this.getClient();
            const sheets = google.sheets({ version: 'v4', auth: client });

            // 1. Check if 'πŸ“… Calendar' tab exists
            const spreadsheet = await sheets.spreadsheets.get({
                spreadsheetId: this.sheetId
            });

            const calendarTab = spreadsheet.data.sheets.find(s => s.properties.title === 'πŸ“… Calendar');
            if (!calendarTab) {
                // Create tab + headers
                await sheets.spreadsheets.batchUpdate({
                    spreadsheetId: this.sheetId,
                    requestBody: {
                        requests: [
                            { addSheet: { properties: { title: 'πŸ“… Calendar' } } }
                        ]
                    }
                });

                const headers = ['ID', 'Date', 'Platform', 'Status', 'Caption EN', 'Caption ID', 'Image URL', 'Scheduled For', 'Zernio Post ID', 'Likes', 'Comments', 'Shares'];
                await sheets.spreadsheets.values.update({
                    spreadsheetId: this.sheetId,
                    range: 'πŸ“… Calendar!A1:L1',
                    valueInputOption: 'RAW',
                    requestBody: { values: [headers] }
                });
            }

            // 2. Bonus: Create 'πŸ“Š Analytics' tab for trending data
            const analyticsTab = spreadsheet.data.sheets.find(s => s.properties.title === 'πŸ“Š Analytics');
            if (!analyticsTab) {
                await sheets.spreadsheets.batchUpdate({
                    spreadsheetId: this.sheetId,
                    requestBody: {
                        requests: [{ addSheet: { properties: { title: 'πŸ“Š Analytics' } } }]
                    }
                });
                await sheets.spreadsheets.values.update({
                    spreadsheetId: this.sheetId,
                    range: 'πŸ“Š Analytics!A1:D1',
                    valueInputOption: 'RAW',
                    requestBody: { values: [['Platform', 'Total Posts', 'Avg Likes', 'Followers']] }
                });
            }

            return { 
                success: true, 
                url: `https://docs.google.com/spreadsheets/d/${this.sheetId}/edit` 
            };
        } catch (error) {
            console.error('[Sheets] Init failed:', error.message);
            return { success: false, error: error.message };
        }
    }

    async appendPost(rowData) {
        if (!this.sheetId) return { success: false, error: 'GOOGLE_SHEETS_ID not set' };

        try {
            const client = await this.getClient();
            const sheets = google.sheets({ version: 'v4', auth: client });

            // rowData: [ID, Date, Platform, Status, CaptionEN, CaptionID, ImageURL, ScheduledTime, ZernioID, ...]
            await sheets.spreadsheets.values.append({
                spreadsheetId: this.sheetId,
                range: 'πŸ“… Calendar!A:L',
                valueInputOption: 'RAW',
                insertDataOption: 'INSERT_ROWS',
                requestBody: { values: [rowData] }
            });
            return { success: true };
        } catch (error) {
            console.error('[Sheets] Append failed:', error.message);
            return { success: false, error: error.message };
        }
    }

    async updatePostStatus(zernioPostId, status, analytics = {}) {
        if (!this.sheetId) return { success: false, error: 'GOOGLE_SHEETS_ID not set' };

        try {
            const client = await this.getClient();
            const sheets = google.sheets({ version: 'v4', auth: client });

            // 1. Find the row by Zernio Post ID (Column I = Index 8)
            const response = await sheets.spreadsheets.values.get({
                spreadsheetId: this.sheetId,
                range: 'πŸ“… Calendar!A:L'
            });

            const rows = response.data.values;
            const rowIndex = rows.findIndex(r => r[8] === zernioPostId);
            if (rowIndex === -1) return { success: false, error: 'Post ID not found in sheet' };

            const realIndex = rowIndex + 1; // 1-indexed for range

            // 2. Update status (Column D = index 3) and analytics (J,K,L = 9,10,11)
            await sheets.spreadsheets.values.update({
                spreadsheetId: this.sheetId,
                range: `πŸ“… Calendar!D${realIndex}:L${realIndex}`,
                valueInputOption: 'RAW',
                requestBody: {
                    values: [[
                        status,
                        rows[rowIndex][4], rows[rowIndex][5], rows[rowIndex][6], rows[rowIndex][7], rows[rowIndex][8],
                        analytics.likes || '-',
                        analytics.comments || '-',
                        analytics.shares || '-'
                    ]]
                }
            });

            return { success: true };
        } catch (error) {
            console.error('[Sheets] Update failed:', error.message);
            return { success: false, error: error.message };
        }
    }
    async appendSale(txn, offer) {
        try {
            const client = await this.getClient();
            if (!client) return { success: false, error: 'No Google auth' };
            const sheets = google.sheets({ version: 'v4', auth: client });

            // Ensure Revenue tab exists
            const spreadsheet = await sheets.spreadsheets.get({ spreadsheetId: this.sheetId });
            const revenueTab = spreadsheet.data.sheets.find(s => s.properties.title === 'πŸ’° Revenue');
            if (!revenueTab) {
                await sheets.spreadsheets.batchUpdate({
                    spreadsheetId: this.sheetId,
                    requestBody: { requests: [{ addSheet: { properties: { title: 'πŸ’° Revenue' } } }] }
                });
                // Add headers
                await sheets.spreadsheets.values.update({
                    spreadsheetId: this.sheetId,
                    range: 'πŸ’° Revenue!A1:G1',
                    valueInputOption: 'RAW',
                    requestBody: { values: [['Date', 'Transaction ID', 'Offer', 'Amount (IDR)', 'Buyer', 'Pillar', 'Offer ID']] }
                });
            }

            // Append sale row
            await sheets.spreadsheets.values.append({
                spreadsheetId: this.sheetId,
                range: 'πŸ’° Revenue!A:G',
                valueInputOption: 'RAW',
                requestBody: {
                    values: [[
                        txn.ts,
                        txn.id,
                        offer.title,
                        txn.amount,
                        txn.buyerEmail || 'Anonymous',
                        offer.sourcePillar || 'general',
                        offer.id
                    ]]
                }
            });

            return { success: true };
        } catch (error) {
            console.error('[Sheets] appendSale failed:', error.message);
            return { success: false, error: error.message };
        }
    }
}

module.exports = new SheetsModule();