Skip to content

Commit

Permalink
add last sync functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-fidd committed Jan 28, 2025
1 parent f35f541 commit 28d05e6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 12 deletions.
7 changes: 1 addition & 6 deletions packages/desktop-client/src/components/Modals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,7 @@ export function Modals() {
return <PostsOfflineNotification key={name} />;

case 'synced-account-edit':
return (
<EditSyncAccount
key={name}
account={options.account}
/>
);
return <EditSyncAccount key={name} account={options.account} />;

case 'account-menu':
return (
Expand Down
19 changes: 17 additions & 2 deletions packages/desktop-client/src/components/banksync/AccountRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
import React, { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { format } from 'loot-core/src/shared/months';
import { type AccountEntity } from 'loot-core/src/types/models';

import { useDateFormat } from '../../hooks/useDateFormat';
import { theme } from '../../style';
import { Button } from '../common/Button2';
import { Row, Cell } from '../table';

const tsToString = (ts, dateFormat) => {
if (!ts) return 'Unknown';

const tsObj = new Date(parseInt(ts, 10));
const date = format(tsObj, dateFormat);

return `${date} ${tsObj.toLocaleTimeString()}`;
};

type AccountRowProps = {
account: AccountEntity;
hovered: boolean;
Expand All @@ -20,6 +31,10 @@ export const AccountRow = memo(
const { t } = useTranslation();
const backgroundFocus = hovered;

const dateFormat = useDateFormat() || 'MM/dd/yyyy';

const lastSync = tsToString(account.last_sync, dateFormat);

return (
<Row
height="auto"
Expand Down Expand Up @@ -53,11 +68,11 @@ export const AccountRow = memo(

<Cell
name="stage"
width={140}
width={200}
plain
style={{ color: theme.tableText, padding: '10px' }}
>
{account.account_sync_source ? 'Unknown' : ''}
{lastSync}
</Cell>

{account.account_sync_source ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function AccountsHeader({ unlinked }: AccountsHeaderProps) {
/>
<Cell
value={t('Last sync')}
width={100}
width={160}
style={{ paddingLeft: '10px' }}
/>
<Cell value="" width={100} style={{ paddingLeft: '10px' }} />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN TRANSACTION;

ALTER TABLE accounts ADD COLUMN last_sync text;

COMMIT;
2 changes: 2 additions & 0 deletions packages/loot-core/src/client/accounts/accountsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ function handleSyncResponse(
resMatchedTransactions.push(...matchedTransactions);
resUpdatedAccounts.push(...updatedAccounts);

dispatch(getAccounts());

return newTransactions.length > 0 || matchedTransactions.length > 0;
}

Expand Down
1 change: 1 addition & 0 deletions packages/loot-core/src/server/aql/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const schema = {
account_id: f('string'),
official_name: f('string'),
account_sync_source: f('string'),
last_sync: f('string'),
},
categories: {
id: f('id'),
Expand Down
10 changes: 7 additions & 3 deletions packages/loot-core/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ handlers['gocardless-create-web-token'] = async function ({
}
};

function handleSyncResponse(
async function handleSyncResponse(
res,
acct,
newTransactions,
Expand All @@ -1060,6 +1060,10 @@ function handleSyncResponse(
if (added.length > 0) {
updatedAccounts.push(acct.id);
}

const ts = new Date().getTime().toString();
const id = acct.id;
await db.runQuery(`UPDATE accounts SET last_sync = ? WHERE id = ?`, [ts, id]);
}

function handleSyncError(err, acct) {
Expand Down Expand Up @@ -1127,7 +1131,7 @@ handlers['accounts-bank-sync'] = async function ({ ids = [] }) {
acct.bankId,
);

handleSyncResponse(
await handleSyncResponse(
res,
acct,
newTransactions,
Expand Down Expand Up @@ -1196,7 +1200,7 @@ handlers['simplefin-batch-sync'] = async function ({ ids = [] }) {
),
);
} else {
handleSyncResponse(
await handleSyncResponse(
account.res,
accounts.find(a => a.id === account.accountId),
newTransactions,
Expand Down
1 change: 1 addition & 0 deletions packages/loot-core/src/types/models/account.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type _SyncFields<T> = {
balance_available: T extends true ? number : null;
balance_limit: T extends true ? number : null;
account_sync_source: T extends true ? AccountSyncSource : null;
last_sync: T extends true ? string : null;
};

export type AccountSyncSource = 'simpleFin' | 'goCardless';

0 comments on commit 28d05e6

Please sign in to comment.