fix: isolate Today summary drafts by account
This commit is contained in:
parent
b3a359760c
commit
abc736d32e
|
|
@ -1,16 +1,27 @@
|
||||||
function createTodaySummary({ storage = null, getLogin = () => '', share = null, copy = null } = {}) {
|
function createTodaySummary({ storage = null, getLogin = () => '', share = null, copy = null } = {}) {
|
||||||
let draft = null;
|
let draft = null;
|
||||||
|
let draftKey = '';
|
||||||
|
|
||||||
const storageKey = () => {
|
const storageKey = () => {
|
||||||
const login = String(getLogin() || '').trim().toLowerCase();
|
const login = String(getLogin() || '').trim().toLowerCase();
|
||||||
return login ? 'stackchain.today-summary-draft.v1.' + login : '';
|
return login ? 'stackchain.today-summary-draft.v1.' + login : '';
|
||||||
};
|
};
|
||||||
|
const syncAccount = () => {
|
||||||
|
const key = storageKey();
|
||||||
|
if (key !== draftKey) {
|
||||||
|
draft = null;
|
||||||
|
draftKey = key;
|
||||||
|
}
|
||||||
|
};
|
||||||
const copyRows = rows => rows.map(row => ({ ...row }));
|
const copyRows = rows => rows.map(row => ({ ...row }));
|
||||||
const snapshot = () => draft ? {
|
const snapshot = () => {
|
||||||
...draft,
|
syncAccount();
|
||||||
worked:copyRows(draft.worked),
|
return draft ? {
|
||||||
tomorrow:copyRows(draft.tomorrow),
|
...draft,
|
||||||
} : null;
|
worked:copyRows(draft.worked),
|
||||||
|
tomorrow:copyRows(draft.tomorrow),
|
||||||
|
} : null;
|
||||||
|
};
|
||||||
const persist = () => {
|
const persist = () => {
|
||||||
const key = storageKey();
|
const key = storageKey();
|
||||||
if (storage && key && draft) storage.setItem(key, JSON.stringify(draft));
|
if (storage && key && draft) storage.setItem(key, JSON.stringify(draft));
|
||||||
|
|
@ -58,6 +69,7 @@ function createTodaySummary({ storage = null, getLogin = () => '', share = null,
|
||||||
|
|
||||||
return {
|
return {
|
||||||
begin(worked, tomorrow) {
|
begin(worked, tomorrow) {
|
||||||
|
draftKey = storageKey();
|
||||||
draft = {
|
draft = {
|
||||||
worked:(worked || []).filter(row => row?.identity).slice(0, 20).map(row => cleanRow(row, true)),
|
worked:(worked || []).filter(row => row?.identity).slice(0, 20).map(row => cleanRow(row, true)),
|
||||||
tomorrow:(tomorrow || []).filter(row => row?.identity).slice(0, 20).map(row => cleanRow(row, false)),
|
tomorrow:(tomorrow || []).filter(row => row?.identity).slice(0, 20).map(row => cleanRow(row, false)),
|
||||||
|
|
@ -69,10 +81,12 @@ function createTodaySummary({ storage = null, getLogin = () => '', share = null,
|
||||||
},
|
},
|
||||||
snapshot,
|
snapshot,
|
||||||
restore() {
|
restore() {
|
||||||
|
draftKey = storageKey();
|
||||||
draft = load();
|
draft = load();
|
||||||
return Boolean(draft);
|
return Boolean(draft);
|
||||||
},
|
},
|
||||||
choose(section, identity, include) {
|
choose(section, identity, include) {
|
||||||
|
syncAccount();
|
||||||
const row = draft?.[section]?.find(candidate => candidate.identity === identity);
|
const row = draft?.[section]?.find(candidate => candidate.identity === identity);
|
||||||
if (!row || typeof include !== 'boolean') return false;
|
if (!row || typeof include !== 'boolean') return false;
|
||||||
row.include = include;
|
row.include = include;
|
||||||
|
|
@ -80,12 +94,14 @@ function createTodaySummary({ storage = null, getLogin = () => '', share = null,
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
includeActuals(include) {
|
includeActuals(include) {
|
||||||
|
syncAccount();
|
||||||
if (!draft || typeof include !== 'boolean') return false;
|
if (!draft || typeof include !== 'boolean') return false;
|
||||||
draft.include_actuals = include;
|
draft.include_actuals = include;
|
||||||
persist();
|
persist();
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
setNote(note) {
|
setNote(note) {
|
||||||
|
syncAccount();
|
||||||
if (!draft) return false;
|
if (!draft) return false;
|
||||||
draft.note = String(note || '').trim().slice(0, 1000);
|
draft.note = String(note || '').trim().slice(0, 1000);
|
||||||
persist();
|
persist();
|
||||||
|
|
@ -93,6 +109,7 @@ function createTodaySummary({ storage = null, getLogin = () => '', share = null,
|
||||||
},
|
},
|
||||||
discard,
|
discard,
|
||||||
text() {
|
text() {
|
||||||
|
syncAccount();
|
||||||
if (!draft) return '';
|
if (!draft) return '';
|
||||||
const sections = [];
|
const sections = [];
|
||||||
const worked = selectedLines(draft.worked, draft.include_actuals);
|
const worked = selectedLines(draft.worked, draft.include_actuals);
|
||||||
|
|
|
||||||
|
|
@ -153,3 +153,21 @@ summary.begin([{{
|
||||||
process.stdout.write(JSON.stringify({{text:summary.text()}}));
|
process.stdout.write(JSON.stringify({{text:summary.text()}}));
|
||||||
"""
|
"""
|
||||||
assert run_node(script)["text"] == "Today\n- Ship mobile capture"
|
assert run_node(script)["text"] == "Today\n- Ship mobile capture"
|
||||||
|
|
||||||
|
|
||||||
|
def test_summary_draft_is_not_visible_after_the_confirmed_account_changes():
|
||||||
|
script = f"""
|
||||||
|
const createSummary = require({json.dumps(str(TODAY_SUMMARY))});
|
||||||
|
const values = new Map();
|
||||||
|
const storage = {{
|
||||||
|
getItem:key => values.has(key) ? values.get(key) : null,
|
||||||
|
setItem:(key,value) => values.set(key,value),
|
||||||
|
removeItem:key => values.delete(key),
|
||||||
|
}};
|
||||||
|
let login='timmy';
|
||||||
|
const summary=createSummary({{storage,getLogin:()=>login}});
|
||||||
|
summary.begin([{{identity:'issue:acme/mobile:41:',title:'Private work',actual_minutes:42}}], []);
|
||||||
|
login='alexander';
|
||||||
|
process.stdout.write(JSON.stringify({{snapshot:summary.snapshot(),text:summary.text()}}));
|
||||||
|
"""
|
||||||
|
assert run_node(script) == {"snapshot": None, "text": ""}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user