Skip to content

Commit

Permalink
Merge pull request #77 from grassrootseconomics/philip/vouchers-fix
Browse files Browse the repository at this point in the history
fix(vouchers): Fixes error in voucher selection menus.
  • Loading branch information
mango-habanero authored Jun 1, 2023
2 parents 198c7fe + 9a1ae7b commit ae511ed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
1 change: 0 additions & 1 deletion src/db/models/guardian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class Guardian {
`SELECT guardian FROM guardians WHERE account_phone_number = $1 AND guardian = $2`,
[phoneNumber, guardian]
)
console.log(rows)
return rows.length > 0 ? rows[0].guardian : null;
} catch (error: any){
logger.error(`Error selecting guardian: ${error.message}, stack: ${error.stack}.`)
Expand Down
12 changes: 7 additions & 5 deletions src/lib/nats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,19 @@ async function processRegistrationEvent(
const tag = await generateUserTag(data.to, graphql, phoneNumber)
await new AccountService(db, redis).activateOnChain(config.DEFAULT_VOUCHER.ADDRESS, phoneNumber)
const balance = await retrieveWalletBalance(data.to, config.DEFAULT_VOUCHER.ADDRESS, provider)
const voucher = {
address: config.DEFAULT_VOUCHER.ADDRESS,
balance,
symbol: config.DEFAULT_VOUCHER.SYMBOL,
}
const user = await new UserService(phoneNumber, redis).update({
account: {
active_voucher_address: config.DEFAULT_VOUCHER.ADDRESS,
},
tag,
vouchers: {
active: {
address: config.DEFAULT_VOUCHER.ADDRESS,
balance,
symbol: config.DEFAULT_VOUCHER.SYMBOL,
}
active: voucher,
held: [voucher]
}
})

Expand Down
1 change: 0 additions & 1 deletion src/machines/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ async function transferTranslations(context: TransferContext, state: string, tra
});
case 'invalidRecipient':
case 'invalidRecipientWithInvite':
console.log(`STATE: ${state} AND CONTEXT DATA: ${JSON.stringify(data)}`)
return await translate(state, translator, { recipient: data?.recipientEntry});
case 'inviteError':
case 'inviteSuccess':
Expand Down
40 changes: 29 additions & 11 deletions src/machines/voucher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const stateMachine = createMachine<VouchersContext, MachineEvent>({
on: {
BACK: 'loadingHeldVouchers',
TRANSIT: [
{ target: 'exit', cond: 'isOption00' },
{ target: 'accountBlocked', cond: 'isBlocked' },
{ target: 'authorizingSelection' },
]
Expand Down Expand Up @@ -244,7 +245,8 @@ function isSetError(context: VouchersContext, event: any) {

async function loadHeldVouchers(context: VouchersContext) {
const { connections: { graphql, redis }, user: { account, statement, vouchers: { active, held } } } = context
const voucherInfoPromises = (held || [active]).map(async (voucher) => {
const vouchers = new Set([...(held || []), active]);
const voucherInfoPromises = Array.from(vouchers).map(async (voucher) => {
const info = await getVoucherByAddress(voucher.address, graphql, redis.persistent)
if(info){
return {
Expand Down Expand Up @@ -346,13 +348,18 @@ function isValidVoucherOption(context: VouchersContext, event: any) {

// Use for loop instead of find to break early when a match is found
for (const voucher of heldVouchers) {
// Check if the voucher can be split and do the split
if (voucher.includes('. ')) {
const [index, symbol] = voucher.split('. ');
// Get the tokens from each voucher string
const tokens = voucher.split("\n")

// Check if the index or symbol matches the input and return true if found
if (index === input || (symbol.includes(" ") && symbol.split(" ")[0].toLowerCase() === input)) {
return true;
// Check if the voucher can be split and do the split
for (const token of tokens) {
if (token.includes('. ')) {
const [index, symbol] = token.split('. ');

// Check if the index or symbol matches the input and return true if found
if (index === input || (symbol.includes(" ") && symbol.split(" ")[0].toLowerCase() === input)) {
return true;
}
}
}
}
Expand Down Expand Up @@ -399,10 +406,21 @@ function saveVoucherSelection(context: VouchersContext, event: any) {
throw new MachineError(ContextError.MALFORMED_CONTEXT, "Held vouchers are missing from context data.");
}

const selectedVoucher = context.data.heldVouchers.find((voucher) => {
const [index, symbol] = voucher.split('. ');
return index === input || symbol.split(" ")[0].toLowerCase() === input;
});
let selectedVoucher = null;
for (const voucher of context.data.heldVouchers) {

const tokens = voucher.split("\n")

for (const token of tokens) {
if (token.includes('. ')) {
const [index, symbol] = token.split('. ');
if (index === input || (symbol.includes(" ") && symbol.split(" ")[0].toLowerCase() === input)) {
selectedVoucher = token;
break;
}
}
}
}

if (selectedVoucher) {
const [_, symbol] = selectedVoucher.split('. ');
Expand Down
1 change: 0 additions & 1 deletion src/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ export class AccountService {
}

export async function getPhoneNumberFromAddress(address: string, db: PostgresDb, redis: RedisClient) {
console.log(`Getting phone number for address: ${address}`);
let phoneNumber = await redis.get(`address-phone-${address}`);
if (phoneNumber) {
return phoneNumber;
Expand Down

0 comments on commit ae511ed

Please sign in to comment.