Skip to content

Commit d5df63a

Browse files
committed
saving more settings
1 parent bb7da04 commit d5df63a

1 file changed

Lines changed: 39 additions & 28 deletions

File tree

src/routes/(protected)/populate/+page.server.ts

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,21 @@ export const load: PageServerLoad = async ({ locals }) => {
4444
const username = user?.username || 'unknown';
4545
const defaultPrefix = getUsernamePrefix(username);
4646

47-
// Try to load saved prefix from OBP Personal Data Field, then session, then derive from username
47+
// Try to load saved settings from OBP Personal Data Fields, then session
4848
let bankIdPrefix = defaultPrefix;
49+
let countryCode = 'BW';
50+
let currency = 'BWP';
4951
if (accessToken) {
5052
const client = new OBPClient(env.PUBLIC_OBP_BASE_URL, 'v6.0.0', accessToken);
5153
try {
5254
const fields = await client.getPersonalDataFields();
53-
const saved = (fields.user_attributes || []).find(
54-
(f) => f.name === 'sandbox_populator_last_bank_id_prefix'
55-
);
56-
if (saved?.value) {
57-
bankIdPrefix = saved.value;
58-
}
55+
const attrs = fields.user_attributes || [];
56+
const savedPrefix = attrs.find((f) => f.name === 'sandbox_populator_last_bank_id_prefix');
57+
if (savedPrefix?.value) bankIdPrefix = savedPrefix.value;
58+
const savedCountry = attrs.find((f) => f.name === 'sandbox_populator_last_country');
59+
if (savedCountry?.value) countryCode = savedCountry.value;
60+
const savedCurrency = attrs.find((f) => f.name === 'sandbox_populator_last_currency');
61+
if (savedCurrency?.value) currency = savedCurrency.value;
5962
} catch (e) {
6063
logger.warn('Could not load personal data fields');
6164
}
@@ -205,8 +208,8 @@ export const load: PageServerLoad = async ({ locals }) => {
205208
defaults: {
206209
numBanks: 2,
207210
numAccountsPerBank: 5,
208-
countryCode: 'BW',
209-
currency: 'BWP',
211+
countryCode,
212+
currency,
210213
bankIdPrefix
211214
},
212215
existing
@@ -250,37 +253,45 @@ export const actions: Actions = {
250253
accessToken
251254
);
252255

253-
// Save the last used prefix to session and OBP Personal Data Field
256+
// Save settings to session and OBP Personal Data Fields
254257
try {
255258
await session.setData({
256259
...session.data,
257260
sandbox_populator_last_bank_id_prefix: bankIdPrefix
258261
});
259262
await session.save();
260263
} catch (e) {
261-
logger.warn('Could not save bank ID prefix to session');
264+
logger.warn('Could not save settings to session');
262265
}
263266
try {
264267
const fields = await client.getPersonalDataFields();
265-
const existing = (fields.user_attributes || []).find(
266-
(f) => f.name === 'sandbox_populator_last_bank_id_prefix'
267-
);
268-
if (existing) {
269-
await client.updatePersonalDataField(existing.user_attribute_id, {
270-
name: 'sandbox_populator_last_bank_id_prefix',
271-
type: 'STRING',
272-
value: bankIdPrefix
273-
});
274-
} else {
275-
await client.createPersonalDataField({
276-
name: 'sandbox_populator_last_bank_id_prefix',
277-
type: 'STRING',
278-
value: bankIdPrefix
279-
});
268+
const attrs = fields.user_attributes || [];
269+
270+
const settingsToSave = [
271+
{ name: 'sandbox_populator_last_bank_id_prefix', value: bankIdPrefix },
272+
{ name: 'sandbox_populator_last_country', value: countryCode },
273+
{ name: 'sandbox_populator_last_currency', value: currency },
274+
];
275+
276+
for (const setting of settingsToSave) {
277+
const existing = attrs.find((f) => f.name === setting.name);
278+
if (existing) {
279+
await client.updatePersonalDataField(existing.user_attribute_id, {
280+
name: setting.name,
281+
type: 'STRING',
282+
value: setting.value
283+
});
284+
} else {
285+
await client.createPersonalDataField({
286+
name: setting.name,
287+
type: 'STRING',
288+
value: setting.value
289+
});
290+
}
280291
}
281-
logger.info(`Saved bank ID prefix "${bankIdPrefix}" to personal data field`);
292+
logger.info(`Saved populate settings to personal data fields`);
282293
} catch (e) {
283-
logger.warn('Could not save bank ID prefix to OBP personal data field');
294+
logger.warn('Could not save settings to OBP personal data fields');
284295
}
285296

286297
const createCounterparties = formData.get('createCounterparties') === 'on';

0 commit comments

Comments
 (0)