mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
add bouncing rate + adjustments
This commit is contained in:
@@ -1,9 +1,62 @@
|
||||
|
||||
import { ProjectModel } from "@schema/ProjectSchema";
|
||||
import { UserModel } from "@schema/UserSchema";
|
||||
import { UserSettingsModel } from "@schema/UserSettings";
|
||||
import { EventModel } from '@schema/metrics/EventSchema';
|
||||
import { VisitModel } from "@schema/metrics/VisitSchema";
|
||||
|
||||
import { google } from 'googleapis';
|
||||
|
||||
const { GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig()
|
||||
|
||||
async function exportToGoogle(data: string, user_id: string) {
|
||||
|
||||
const user = await UserModel.findOne({ _id: user_id }, { google_tokens: true });
|
||||
|
||||
const authClient = new google.auth.OAuth2({
|
||||
clientId: GOOGLE_AUTH_CLIENT_ID,
|
||||
clientSecret: GOOGLE_AUTH_CLIENT_SECRET
|
||||
})
|
||||
|
||||
authClient.setCredentials({ access_token: user?.google_tokens?.access_token, refresh_token: user?.google_tokens?.refresh_token });
|
||||
|
||||
const sheets = google.sheets({ version: 'v4', auth: authClient });
|
||||
|
||||
try {
|
||||
const createSheetResponse = await sheets.spreadsheets.create({
|
||||
requestBody: {
|
||||
properties: {
|
||||
title: 'Text export'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const spreadsheetId = createSheetResponse.data.spreadsheetId;
|
||||
|
||||
await sheets.spreadsheets.values.update({
|
||||
spreadsheetId: spreadsheetId as string,
|
||||
range: 'Sheet1!A1',
|
||||
requestBody: {
|
||||
values: data.split('\n').map(e => {
|
||||
return e.split(',')
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
return { ok: true }
|
||||
|
||||
} catch (error: any) {
|
||||
|
||||
console.error('Error creating Google Sheet from CSV:', error);
|
||||
|
||||
if (error.response && error.response.status === 401) {
|
||||
return { error: 'Auth error, try to logout and login again' }
|
||||
}
|
||||
|
||||
return { error: error.message.toString() }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
@@ -69,6 +122,15 @@ export default defineEventHandler(async event => {
|
||||
return content.join(',');
|
||||
}).join('\n');
|
||||
|
||||
|
||||
|
||||
const isGoogle = getHeader(event, 'x-google-export');
|
||||
|
||||
if (isGoogle === 'true') {
|
||||
const data = await exportToGoogle(result, userData.id);
|
||||
return data;
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return '';
|
||||
|
||||
Reference in New Issue
Block a user