2019-07-31 17:01:18 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
package marketingweb
|
2019-07-31 17:01:18 +01:00
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-11-06 18:37:53 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
"storj.io/storj/satellite/rewards"
|
|
|
|
)
|
2019-08-06 15:41:25 +01:00
|
|
|
|
2019-07-31 17:01:18 +01:00
|
|
|
// OrganizedOffers contains a list of offers organized by status.
|
|
|
|
type OrganizedOffers struct {
|
2019-11-05 12:58:09 +00:00
|
|
|
Active rewards.Offer
|
|
|
|
Default rewards.Offer
|
|
|
|
Done rewards.Offers
|
2019-07-31 17:01:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenSourcePartner contains all data for an Open Source Partner.
|
|
|
|
type OpenSourcePartner struct {
|
2019-11-05 12:58:09 +00:00
|
|
|
rewards.PartnerInfo
|
2019-07-31 17:01:18 +01:00
|
|
|
PartnerOffers OrganizedOffers
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartnerSet contains a list of Open Source Partners.
|
|
|
|
type PartnerSet []OpenSourcePartner
|
|
|
|
|
|
|
|
// OfferSet provides a separation of marketing offers by type.
|
|
|
|
type OfferSet struct {
|
|
|
|
ReferralOffers OrganizedOffers
|
|
|
|
FreeCredits OrganizedOffers
|
|
|
|
PartnerTables PartnerSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// OrganizeOffersByStatus organizes offers by OfferStatus.
|
2019-11-05 12:58:09 +00:00
|
|
|
func (server *Server) OrganizeOffersByStatus(offers rewards.Offers) OrganizedOffers {
|
2019-07-31 17:01:18 +01:00
|
|
|
var oo OrganizedOffers
|
|
|
|
|
|
|
|
for _, offer := range offers {
|
|
|
|
switch offer.Status {
|
2019-11-05 12:58:09 +00:00
|
|
|
case rewards.Active:
|
2019-11-06 18:37:53 +00:00
|
|
|
if !oo.Active.IsZero() {
|
|
|
|
server.log.Error("duplicate active")
|
|
|
|
}
|
2019-07-31 17:01:18 +01:00
|
|
|
oo.Active = offer
|
2019-11-05 12:58:09 +00:00
|
|
|
case rewards.Default:
|
2019-11-06 18:37:53 +00:00
|
|
|
if !oo.Active.IsZero() {
|
|
|
|
server.log.Error("duplicate default")
|
|
|
|
}
|
2019-07-31 17:01:18 +01:00
|
|
|
oo.Default = offer
|
2019-11-05 12:58:09 +00:00
|
|
|
case rewards.Done:
|
2019-07-31 17:01:18 +01:00
|
|
|
oo.Done = append(oo.Done, offer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oo
|
|
|
|
}
|
|
|
|
|
|
|
|
// OrganizeOffersByType organizes offers by OfferType.
|
2019-11-05 12:58:09 +00:00
|
|
|
func (server *Server) OrganizeOffersByType(offers rewards.Offers) OfferSet {
|
2019-07-31 17:01:18 +01:00
|
|
|
var (
|
2019-11-05 12:58:09 +00:00
|
|
|
fc, ro, p rewards.Offers
|
2019-07-31 17:01:18 +01:00
|
|
|
offerSet OfferSet
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, offer := range offers {
|
|
|
|
switch offer.Type {
|
2019-11-05 12:58:09 +00:00
|
|
|
case rewards.FreeCredit:
|
2019-07-31 17:01:18 +01:00
|
|
|
fc = append(fc, offer)
|
2019-11-05 12:58:09 +00:00
|
|
|
case rewards.Referral:
|
2019-07-31 17:01:18 +01:00
|
|
|
ro = append(ro, offer)
|
2019-11-05 12:58:09 +00:00
|
|
|
case rewards.Partner:
|
2019-07-31 17:01:18 +01:00
|
|
|
p = append(p, offer)
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
offerSet.FreeCredits = server.OrganizeOffersByStatus(fc)
|
|
|
|
offerSet.ReferralOffers = server.OrganizeOffersByStatus(ro)
|
|
|
|
offerSet.PartnerTables = server.organizePartnerData(p)
|
2019-07-31 17:01:18 +01:00
|
|
|
return offerSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// createPartnerSet generates a PartnerSet from the config file.
|
2019-11-05 12:58:09 +00:00
|
|
|
func (server *Server) createPartnerSet() PartnerSet {
|
2019-11-06 18:37:53 +00:00
|
|
|
all, err := server.partners.All(context.TODO()) // TODO: don't ignore error
|
|
|
|
if err != nil {
|
|
|
|
server.log.Error("failed to load all partners", zap.Error(err))
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-06 15:41:25 +01:00
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
var ps PartnerSet
|
|
|
|
for _, partner := range all {
|
2019-07-31 17:01:18 +01:00
|
|
|
ps = append(ps, OpenSourcePartner{
|
2019-11-05 12:58:09 +00:00
|
|
|
PartnerInfo: partner,
|
2019-07-31 17:01:18 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return ps
|
|
|
|
}
|
|
|
|
|
|
|
|
// matchOffersToPartnerSet assigns offers to the partner they belong to.
|
2019-11-05 12:58:09 +00:00
|
|
|
func (server *Server) matchOffersToPartnerSet(offers rewards.Offers, partnerSet PartnerSet) PartnerSet {
|
2019-07-31 17:01:18 +01:00
|
|
|
for i := range partnerSet {
|
2019-11-05 12:58:09 +00:00
|
|
|
var partnerOffersByName rewards.Offers
|
2019-07-31 17:01:18 +01:00
|
|
|
|
|
|
|
for _, o := range offers {
|
|
|
|
if o.Name == partnerSet[i].PartnerInfo.Name {
|
|
|
|
partnerOffersByName = append(partnerOffersByName, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:58:09 +00:00
|
|
|
partnerSet[i].PartnerOffers = server.OrganizeOffersByStatus(partnerOffersByName)
|
2019-07-31 17:01:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return partnerSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// organizePartnerData returns a list of Open Source Partners
|
|
|
|
// whose offers have been organized by status, type, and
|
|
|
|
// assigned to the correct partner.
|
2019-11-05 12:58:09 +00:00
|
|
|
func (server *Server) organizePartnerData(offers rewards.Offers) PartnerSet {
|
|
|
|
partnerData := server.matchOffersToPartnerSet(offers, server.createPartnerSet())
|
2019-07-31 17:01:18 +01:00
|
|
|
return partnerData
|
|
|
|
}
|