Discount Coupons use case with OpenWhisk Conductors and Hypi

Online shopping has become commonplace. E-commerce websites offer discount coupons on products during online shopping.

In this post, we will look at Discount coupon use case implementation using OpenWhisk Conductors and Hypi’s serverless function. Conductors decide the series of actions at run time. There can be different types of discount coupons. So, conductors are the best choice to implement this common use case.

Configure the OpenWhisk to point towards Hypi domain.

wsk property set --apihost "https://fn.hypi.app" --auth "controversially.apps.hypi.app:<Auth Key>"
ok: whisk auth set. Run 'wsk property get --auth' to see the new value.
ok: whisk API host set to https://fn.hypi.app

Let’s say we have two types of discount coupons. The first coupon offers a percentage discount and the second one offers an amount discount. We can use JavaScript to create conductor actions.

The first action calculates the final price after applying a percentage discount. If the product price is 1000 and the discount is 20%, the final price after discount would be 1000 – (1000*20/100) i.e. 800.

//PercentDiscountPrice.js

function main(args) {
    var price = args.price;
    var dsc = args.dsc;
    var discount = price*dsc/100;
    var finalprice = price-discount;
    return { finalprice : finalprice, price : price };
}
wsk action create percentprice PercentDiscountPrice.js
ok: created action percentprice

The second action calculates the final price after applying the amount discount. If the product price is 1000 and the discount is 100, the final price would be 1000-100 = 900.

//AmountDiscountPrice.js
function main(args) {
    var price = args.price;
    var dsc = args.dsc;
    var finalprice = price-dsc;
    return { finalprice : finalprice, price : price };
}
wsk action create amountprice AmountDiscountPrice.js
ok: created action amountprice

The third action calculates saving amount after discount, i.e. original price - final price

//SavingAmount.js
function main(args) {
    var finalprice= args.finalprice;
    var price = args.price;
    var savngamt = price-finalprice;
    return {finalprice : finalprice, savngamt : savngamt};
}
wsk action create svngamnt SavingAmount.js
ok: created action svngamnt

Ok! all the three actions are in place! Let’s create conductor action now.

//Discount.js
function main(params) {
    switch (params.step) {
        case 0: return { action: 'percentprice', params, state: { step: 2 } }
        case 1: return { action: 'amountprice', params, state: { step: 2 } }
        case 2: return { action: 'svngamnt', params, state: { step: 3 } }
        case 3:
        default: return { finalprice:params.finalprice, savngamt:params.savngamt }
    }
}
wsk action create discount Discount.js -a conductor true
ok: created action discount

Here, we pass step number at run time which decides the sequence of actions. If there is a percentage discount, percentprice and svngamnt actions will get invoked. The initial step is 0 for this sequence.

If there is an amount discount, amountprice and svngamnt actions will get invoked. The initial step is 1 for this sequence.

In both cases, the final price of the product after discount and calculated saving will be returned.

wsk action invoke discount -r -p price 1800 -p dsc 50 -p step 0
{
    "finalprice": 900,
    "savngamt": 900
}

wsk action invoke discount -r -p price 1800 -p dsc 200 -p step 1
{
    "finalprice": 1600,
    "savngamt": 200
}

Alright! Let’s use the conductor action in Hypi’s serverless function getDiscountPrice. The function is declared in the App’s schema.

#Hypi's serverless function:
Query {
  getDiscountPrice(price: Int,dsc: Int, step: Int): Json @tan(type:OpenWhisk, name:"discount")
}

Execute the function in the GraphQL Playground.

#Sequence1
{
  getDiscountPrice(price:1200, dsc: 120, step:1)
}
#Result
{
  "data": {
    "getDiscountPrice": {
      "finalprice": 1080,
      "savngamt": 120
    }
  }
}
#Sequence2
{
  getDiscountPrice(price:2000, dsc: 10, step:0)
}
#Result
{
  "data": {
    "getDiscountPrice": {
      "finalprice": 1800,
      "savngamt": 200
    }
  }
}

Concluding Note

Whenever there is a sequence of actions to be decided at run time, OpenWhisk conductors are the best choice.
Discount coupon is the simplest use case you may try using Hypi’s serverless functions and OpenWhisk conductors.

If you have any suggestions/feedback do let us know!