better code: less changing of data

Massimo Melina committed Jan 7, 2023 at 17:06 UTC 6eebf77674ec9cac6edbc637771ff7fcf5d98515
1 file changed +24 -21
src/perm.ts
+24 -21
@@ -71,30 +71,34 @@ export async function updateAccount(account: Account, changer?:Changer) {
71 console.log('please reset password for account', username)
72 process.exit(1)
73 }
74 - if (account.belongs)
75 - account.belongs = wantArray(account.belongs).filter(b =>
76 - b in accounts // at this stage the group record may still be null if specified later in the file
77 - || console.error(`account ${username} belongs to non-existing ${b}`) )
74 + if (account.belongs) {
75 + account.belongs = wantArray(account.belongs)
76 + _.remove(account.belongs, b => {
77 + if (b in accounts) return
78 + console.error(`account ${username} belongs to non-existing ${b}`)
79 + return true
80 + })
81 + }
82 if (was !== JSON.stringify(account))
83 saveAccountsAsap()
84 }
85
82 -const saveAccountsAsap = saveConfigAsap
86 +const saveAccountsAsap = () => { saveConfigAsap().then() }
87
84 -export const accountsConfig = defineConfig<Accounts>('accounts', {})
85 -accountsConfig.sub(async v => {
86 - // we should validate content here
87 - accounts = v // keep local reference
88 - await Promise.all(_.map(accounts, async (rec,k) => {
88 +export const accountsConfig = defineConfig('accounts', {} as Accounts)
89 +accountsConfig.sub(obj => {
90 + // consider some validation here
91 + _.each(accounts = obj, (rec,k) => {
92 const norm = normalizeUsername(k)
90 - if (!rec) // an empty object in yaml is stored as null
91 - rec = accounts[norm] = { username: norm }
92 - else
93 - if (objRenameKey(accounts, k, norm))
93 + if (rec?.username !== norm) {
94 + if (!rec) // an empty object in yaml is parsed as null
95 + rec = obj[norm] = { username: norm }
96 + else if (objRenameKey(obj, k, norm))
97 saveAccountsAsap()
95 - setHidden(rec, { username: norm })
96 - await updateAccount(rec) // work password fields
97 - }))
98 + setHidden(rec, { username: norm })
99 + }
100 + updateAccount(rec).then() // work password fields
101 + })
102 })
103
104 export function normalizeUsername(username: string) {
@@ -135,7 +139,6 @@ export function addAccount(username: string, props: Partial<Account>) {
139 const copy: Account = setHidden(filteredProps, { username }) // have the field in the object but hidden so that stringification won't include it
140 accountsConfig.set(accounts =>
141 Object.assign(accounts, { [username]: copy }))
138 - saveAccountsAsap().then()
142 return copy
143 }
144
@@ -150,7 +153,7 @@ export function setAccount(username: string, changes: Partial<Account>) {
153 Object.assign(acc, rest)
154 if (changes.username)
155 renameAccount(username, changes.username)
153 - saveAccountsAsap().then()
156 + saveAccountsAsap()
157 return acc
158 }
159
@@ -159,7 +162,7 @@ export function delAccount(username: string) {
162 return false
163 accountsConfig.set(accounts =>
164 Object.assign(accounts, { [normalizeUsername(username)]: undefined }))
162 - saveAccountsAsap().then()
165 + saveAccountsAsap()
166 return true
167 }
168
@@ -190,5 +193,5 @@ export function accountCanLoginAdmin(account: Account) {
193 }
194
195 export function anyAccountCanLoginAdmin() {
193 - return Object.values(accounts).find(accountCanLoginAdmin)
196 + return Boolean(_.find(accountsConfig.get(), accountCanLoginAdmin))
197 }