Skip to content

Commit

Permalink
Add flashsale endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskaranbir committed Nov 15, 2018
1 parent a5b0440 commit 41ee00a
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 0 deletions.
26 changes: 26 additions & 0 deletions gql/entity/flashsale/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Usage examples
---

### Mutations

* #### InventoryInsert
```graphql
mutation{
SaleInsert(
saleID: "cdc7a14c-19e3-488e-8c4e-22d91fd42ef1",
items: [
{
itemID: "39322979-d33b-4504-ba90-f2e427bdd72b",
weight: 12.40,
lot: "test-lot",
upc: "test-upc",
sku: "test-sku"
}
]
timestamp: 1539222685400,
)
{
timestamp
}
}
```
26 changes: 26 additions & 0 deletions gql/entity/flashsale/mutations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package flashsale

import (
"github.com/TerrexTech/go-apigateway/gql/entity/flashsale/resolver"
"github.com/graphql-go/graphql"
)

// Mutations are GraphQL mutations for Inventory.
var Mutations = map[string]*graphql.Field{
"FlashSaleInsert": &graphql.Field{
Type: Sale,
Description: "Inserts item into Inventory",
Args: graphql.FieldConfigArgument{
"flashSaleID": &graphql.ArgumentConfig{
Type: graphql.String,
},
"items": &graphql.ArgumentConfig{
Type: graphql.NewList(SaleInput),
},
"timestamp": &graphql.ArgumentConfig{
Type: graphql.Float,
},
},
Resolve: resolver.Insert,
},
}
94 changes: 94 additions & 0 deletions gql/entity/flashsale/queries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package flashsale

import (
"github.com/TerrexTech/go-apigateway/gql/entity/sale/resolver"
"github.com/graphql-go/graphql"
)

// Queries are GraphQL queries for Inventory
var Queries = map[string]*graphql.Field{
// "InventoryQueryItem": &graphql.Field{
// Type: graphql.NewList(Inventory),
// Description: "Inventory Query",
// Args: graphql.FieldConfigArgument{
// "itemID": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// "dateArrived": &graphql.ArgumentConfig{
// Type: graphql.Float,
// },
// "dateSold": &graphql.ArgumentConfig{
// Type: graphql.Float,
// },
// "deviceID": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// "donateWeight": &graphql.ArgumentConfig{
// Type: graphql.Float,
// },
// "lot": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// "name": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// "origin": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// "price": &graphql.ArgumentConfig{
// Type: graphql.Float,
// },
// "rsCustomerID": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// "salePrice": &graphql.ArgumentConfig{
// Type: graphql.Float,
// },
// "sku": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// "soldWeight": &graphql.ArgumentConfig{
// Type: graphql.Float,
// },
// "timestamp": &graphql.ArgumentConfig{
// Type: graphql.Float,
// },
// "totalWeight": &graphql.ArgumentConfig{
// Type: graphql.Float,
// },
// "upc": &graphql.ArgumentConfig{
// Type: graphql.String,
// },
// "wasteWeight": &graphql.ArgumentConfig{
// Type: graphql.Float,
// },
// },
// Resolve: resolver.QueryItem,
// },
// "InventoryQueryTimestamp": &graphql.Field{
// Type: graphql.NewList(Inventory),
// Description: "Inventory Query by Timestamp",
// Args: graphql.FieldConfigArgument{
// "start": &graphql.ArgumentConfig{
// Type: graphql.Int,
// },
// "end": &graphql.ArgumentConfig{
// Type: graphql.Int,
// },
// "count": &graphql.ArgumentConfig{
// Type: graphql.Int,
// },
// },
// Resolve: resolver.QueryTimestamp,
// },
"FlashSaleQueryCount": &graphql.Field{
Type: graphql.NewList(Sale),
Description: "Returns latest sales as specified in count",
Args: graphql.FieldConfigArgument{
"count": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: resolver.QueryCount,
},
}
121 changes: 121 additions & 0 deletions gql/entity/flashsale/resolver/insert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package resolver

import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"time"

"github.com/TerrexTech/go-apigateway/gql/response"
"github.com/TerrexTech/go-apigateway/gwerrors"
"github.com/TerrexTech/go-apigateway/util"
esmodel "github.com/TerrexTech/go-eventstore-models/model"
"github.com/TerrexTech/uuuid"
"github.com/graphql-go/graphql"
"github.com/pkg/errors"
)

// Insert is the GraphQL resolver for InsertSale GraphQL query.
var Insert = func(params graphql.ResolveParams) (interface{}, error) {
consTopic := os.Getenv("KAFKA_CONSUMER_TOPIC_FLASHSALE")

// Marshal Sale-data
saleJSON, err := json.Marshal(params.Args)
if err != nil {
err = errors.Wrap(err, "InvInsertResponseHandler: Error marshalling credentials into JSON")
return nil, err
}
log.Println(string(saleJSON))

rootValue := params.Info.RootValue.(map[string]interface{})
kf := rootValue["kafkaFactory"].(*util.KafkaFactory)

// CorrelationID
cid, err := uuuid.NewV4()
if err != nil {
err = errors.Wrap(err, "InvInsertResponseHandler: Error generating UUID for cid")
return nil, err
}
eventID, err := uuuid.NewV4()
if err != nil {
err = errors.Wrap(err, "InvInsertResponseHandler: Error generating UUID for InsertSale-Event")
return nil, err
}

// Publish Insert-Event on Kafka Topic
kf.EventProducer() <- &esmodel.Event{
EventAction: "insert",
CorrelationID: cid,
AggregateID: 7,
Data: saleJSON,
NanoTime: time.Now().UnixNano(),
UUID: eventID,
YearBucket: 2018,
}

// Timeout Context
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

krChan, err := kf.EnsureConsumerIO(consTopic, consTopic, false, eventID)
if err != nil {
err = errors.Wrap(err, "InvInsertResponseHandler: Error creating ConsumerIO")
return nil, err
}

select {
case <-ctx.Done():
return nil, errors.New("Timed out")
case kr := <-krChan:
insertInvResp := handleInsertInvResponse(kr)
if insertInvResp != nil {
if insertInvResp.Err == nil {
return insertInvResp.Result, nil
}
insertInvErr := insertInvResp.Err
err = errors.Wrap(insertInvErr.Err, "InvInsertResponseHandler: InsertSale Error")
log.Println(err)
outErr := fmt.Errorf("%d: InsertSale Error", insertInvErr.Code)
return nil, outErr
}
}
return nil, errors.New("Unknown Error")
}

func handleInsertInvResponse(kr esmodel.KafkaResponse) *response.ResolverResponse {
if kr.Error != "" {
err := errors.Wrap(
errors.New(kr.Error),
"InvInsertResponseHandler: Error in KafkaResponse",
)
log.Println(err)
krerr := gwerrors.NewKRError(err, kr.ErrorCode, err.Error())
return &response.ResolverResponse{
Result: nil,
Err: krerr,
}
}

sale := map[string]interface{}{}
err := json.Unmarshal(kr.Result, &sale)
if err != nil {
err = errors.Wrap(
err,
"InvInsertResponseHandler: "+
"Error while Unmarshalling KafkaResponse into flashsale",
)
log.Println(err)
krerr := gwerrors.NewKRError(err, gwerrors.InternalError, err.Error())
return &response.ResolverResponse{
Result: nil,
Err: krerr,
}
}

return &response.ResolverResponse{
Result: sale,
Err: nil,
}
}
Loading

0 comments on commit 41ee00a

Please sign in to comment.