Skip to content

Commit

Permalink
Merge pull request #1097 from niccolopaganini/service_rewrite_2023_em…
Browse files Browse the repository at this point in the history
…ailService

emailService Js -> ts
  • Loading branch information
shankari authored Nov 22, 2023
2 parents 26638a7 + 810c319 commit 35468e5
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 126 deletions.
1 change: 0 additions & 1 deletion www/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import './js/main.js';
import './js/diary.js';
import './js/diary/services.js';
import './js/survey/enketo/answer.js';
import './js/control/emailService.js';
import './js/metrics-factory.js';
import './js/metrics-mappings.js';
import './js/plugin/logger.ts';
4 changes: 2 additions & 2 deletions www/js/control/LogPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { useTranslation } from 'react-i18next';
import { FlashList } from '@shopify/flash-list';
import moment from 'moment';
import AlertBar from './AlertBar';
import { sendEmail } from './emailService';

type loadStats = { currentStart: number; gotMaxIndex: boolean; reachedEnd: boolean };

const LogPage = ({ pageVis, setPageVis }) => {
const { t } = useTranslation();
const { colors } = useTheme();
const EmailHelper = getAngularService('EmailHelper');

const [loadStats, setLoadStats] = useState<loadStats>();
const [entries, setEntries] = useState([]);
Expand Down Expand Up @@ -96,7 +96,7 @@ const LogPage = ({ pageVis, setPageVis }) => {
};

const emailLog = function () {
EmailHelper.sendEmail('loggerDB');
sendEmail('loggerDB');
};

const separator = () => <View style={{ height: 8 }} />;
Expand Down
5 changes: 2 additions & 3 deletions www/js/control/ProfileSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import useAppConfig from '../useAppConfig';
import AlertBar from './AlertBar';
import DataDatePicker from './DataDatePicker';
import PrivacyPolicyModal from './PrivacyPolicyModal';

import { sendEmail } from './emailService';
import { uploadFile } from './uploadService';
import ActionMenu from '../components/ActionMenu';
import SensedPage from './SensedPage';
Expand Down Expand Up @@ -44,7 +44,6 @@ const ProfileSettings = () => {

//angular services needed
const CarbonDatasetHelper = getAngularService('CarbonDatasetHelper');
const EmailHelper = getAngularService('EmailHelper');
const NotificationScheduler = getAngularService('NotificationScheduler');
const ControlHelper = getAngularService('ControlHelper');

Expand Down Expand Up @@ -256,7 +255,7 @@ const ProfileSettings = () => {

const emailLog = function () {
// Passing true, we want to send logs
EmailHelper.sendEmail('loggerDB');
sendEmail('loggerDB');
};

async function updatePrefReminderTime(storeNewVal = true, newTime) {
Expand Down
4 changes: 2 additions & 2 deletions www/js/control/SensedPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { getAngularService } from '../angular-react-helper';
import { useTranslation } from 'react-i18next';
import { FlashList } from '@shopify/flash-list';
import moment from 'moment';
import { sendEmail } from './emailService';

const SensedPage = ({ pageVis, setPageVis }) => {
const { t } = useTranslation();
const { colors } = useTheme();
const EmailHelper = getAngularService('EmailHelper');

/* Let's keep a reference to the database for convenience */
const [DB, setDB] = useState();
const [entries, setEntries] = useState([]);

const emailCache = function () {
EmailHelper.sendEmail('userCacheDB');
sendEmail('userCacheDB');
};

async function updateEntries() {
Expand Down
114 changes: 0 additions & 114 deletions www/js/control/emailService.js

This file was deleted.

52 changes: 52 additions & 0 deletions www/js/control/emailService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import i18next from 'i18next';
import { logDebug, logInfo, logWarn } from '../plugin/logger';

async function hasAccount(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
window['cordova'].plugins['email'].hasAccount((hasAct) => {
resolve(hasAct);
});
});
}

export async function sendEmail(database: string) {
let parentDir = 'unknown';

if (window['cordova'].platformId == 'ios' && !(await hasAccount())) {
alert(i18next.t('email-service.email-account-not-configured'));
return;
}

if (window['cordova'].platformId == 'android') {
parentDir = 'app://databases';
}

if (window['cordova'].platformId == 'ios') {
alert(i18next.t('email-service.email-account-mail-app'));
logDebug(window['cordova'].file.dataDirectory);
parentDir = window['cordova'].file.dataDirectory + '../LocalDatabase';
}

if (parentDir === 'unknown') {
alert('parentDir unexpectedly = ' + parentDir + '!');
}

logInfo('Going to email ' + database);
parentDir = parentDir + '/' + database;

alert(i18next.t('email-service.going-to-email', { parentDir: parentDir }));

let emailConfig = `k.shankari@nrel.gov`;

let emailData = {
to: emailConfig,
attachments: [parentDir],
subject: i18next.t('email-service.email-log.subject-logs'),
body: i18next.t('email-service.email-log.body-please-fill-in-what-is-wrong'),
};

window['cordova'].plugins['email'].open(emailData, () => {
logWarn(`Email app closed while sending,
emailData = ${JSON.stringify(emailData)}`);
});
}
1 change: 0 additions & 1 deletion www/js/ngApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ angular
'emission.services',
'emission.plugin.logger',
'emission.splash.referral',
'emission.services.email',
'emission.main',
'pascalprecht.translate',
'LocalStorageModule',
Expand Down
3 changes: 0 additions & 3 deletions www/json/emailConfig.json.sample

This file was deleted.

0 comments on commit 35468e5

Please sign in to comment.