Denil Parmar
Denil Parmar

Reputation: 156

AWS AppSync VTL Templates failing

I have a Appsync VTL Template and it errors out when I have a #if inside of a #foreach block.

  1. Is this even the right syntax
  2. If not, how can I write this code?
    #foreach($method in $body.paymentMethods)
        #set($transformedMethod = {
            "Id": $method.id,
            "Object": $method.object,
            "BillingDetails": {
                "Email": $method.billing_details.email,
                "Name": $method.billing_details.name,
                "Phone": $method.billing_details.phone
            },
            "Created": $method.created,
            "Customer": $method.customer,
            "Livemode": $method.livemode,
            "Metadata": $utils.toJson($method.metadata),
            "Type": $method.type,
            #if($method.type.equals("us_bank_account"))
            "USBankAccount": {
                "AccountHolderType": $method.us_bank_account.account_holder_type,
                "AccountType": $method.us_bank_account.account_type,
                "BankName": $method.us_bank_account.bank_name,
                "FinancialConnectionsAccount": $method.us_bank_account.financial_connections_account,
                "Fingerprint": $method.us_bank_account.fingerprint,
                "Last4": $method.us_bank_account.last4,
                "Networks": {
                    "Preferred": $method.us_bank_account.networks.preferred,
                    "Supported": $utils.toJson($method.us_bank_account.networks.supported)
                },
                "RoutingNumber": $method.us_bank_account.routing_number,
                "StatusDetails": $method.us_bank_account.status_details
            }
            #end
        })
        $util.qr($paymentMethods.add($transformedMethod))
    #end

I've tried

#if($method.type == "us_bank_account"). This too doesn't work

Here's the source from where I want to obtain the above fields

{
    "isSuccess": true,
    "paymentMethods": [
        {
            "billing_details": {
                "address": {
                    "city": null,
                    "country": null,
                    "line1": null,
                    "line2": null,
                    "postal_code": null,
                    "state": null
                },
                "email": null,
                "name": "Test Account Holder",
                "phone": null
            },
            "created": 1701975288,
            "customer": "cus_P60pU8eyi7OM1T",
            "id": "pm_1OKmiiApDBfa1h4dHUrVeVhm",
            "livemode": false,
            "metadata": {
            },
            "object": "payment_method",
            "type": "us_bank_account",
            "us_bank_account": {
                "account_holder_type": "individual",
                "account_type": "checking",
                "bank_name": "STRIPE TEST BANK",
                "financial_connections_account": null,
                "fingerprint": "vFaCL0w3N7j2qWhO",
                "last4": "6789",
                "networks": {
                    "preferred": "ach",
                    "supported": [
                        "ach"
                    ]
                },
                "routing_number": "110000000",
                "status_details": {
                }
            }
        },
        {
            "billing_details": {
                "address": {
                    "city": "Libertyville",
                    "country": "US",
                    "line1": "1401 Franklin Blvd.",
                    "line2": null,
                    "postal_code": "60048",
                    "state": "IL"
                },
                "email": null,
                "name": "Test",
                "phone": null
            },
            "card": {
                "brand": "visa",
                "checks": {
                    "address_line1_check": "pass",
                    "address_postal_code_check": "pass",
                    "cvc_check": null
                },
                "country": "US",
                "exp_month": 12,
                "exp_year": 2027,
                "fingerprint": "cNo8pU2BD7OMVNAa",
                "funding": "credit",
                "generated_from": null,
                "last4": "4242",
                "networks": {
                    "available": [
                        "visa"
                    ],
                    "preferred": null
                },
                "three_d_secure_usage": {
                    "supported": true
                },
                "wallet": null
            },
            "created": 1701974966,
            "customer": "cus_P60pU8eyi7OM1T",
            "id": "card_1OKmdWApDBfa1h4d0WRWWO7W",
            "livemode": false,
            "metadata": {
            },
            "object": "payment_method",
            "type": "card"
        }
    ]
}

Upvotes: 0

Views: 102

Answers (1)

flambo
flambo

Reputation: 3

This issue might be due to the condition inside the #if directive. If the condition is not correctly evaluating to a boolean value, it could cause an error.
In your template:

#if($method.type.equals("us_bank_account"))

This directive checks if the type property of the current $method object equals the string "us_bank_account". If the type property does not exist or is not a string, this could cause an error.

Upvotes: 0

Related Questions