func TestCreditCardInfoNotSubscribingMember(t *testing.T) { Convey("When a non subscribed user request to get CC", t, func() { withTestServer(t, func(endpoint string) { withStubData(endpoint, func(username, groupName, sessionID string) { group, err := modelhelper.GetGroup(groupName) tests.ResultedWithNoErrorCheck(group, err) err = modelhelper.UpdateGroupPartial( modelhelper.Selector{"_id": group.Id}, modelhelper.Selector{ "$unset": modelhelper.Selector{"payment.customer.id": ""}, }, ) So(err, ShouldBeNil) Convey("Endpoint should return error", func() { _, err := rest.DoRequestWithAuth("GET", endpoint+EndpointCreditCardHas, nil, sessionID) So(err, ShouldNotBeNil) // set the customer id back becase test data callback requires it. err = modelhelper.UpdateGroupPartial( modelhelper.Selector{"_id": group.Id}, modelhelper.Selector{ "$set": modelhelper.Selector{"payment.customer.id": group.Payment.Customer.ID}, }, ) So(err, ShouldBeNil) }) }) }) }) }
// EnsureCustomerForGroup registers a customer for a group if it does not exist, // returns the existing one if created previously func EnsureCustomerForGroup(username string, groupName string, req *stripe.CustomerParams) (*stripe.Customer, error) { group, err := modelhelper.GetGroup(groupName) if err != nil { return nil, err } // if we already have the customer, return it. if group.Payment.Customer.ID != "" { return customer.Get(group.Payment.Customer.ID, nil) } req, err = populateCustomerParams(username, group.Slug, req) if err != nil { return nil, err } cus, err := customer.New(req) if err != nil { return nil, err } if err := modelhelper.UpdateGroupPartial( modelhelper.Selector{"_id": group.Id}, modelhelper.Selector{ "$set": modelhelper.Selector{ "payment.customer.id": cus.ID, }, }, ); err != nil { return nil, err } return cus, nil }
func updateGroupPartially(groupId bson.ObjectId, property string, value string) error { return modelhelper.UpdateGroupPartial( modelhelper.Selector{"_id": groupId}, modelhelper.Selector{ "$set": modelhelper.Selector{ property: value, }, }, ) }
func syncGroupWithCustomerID(cusID string) error { cus, err := customer.Get(cusID, nil) if err != nil { return err } group, err := modelhelper.GetGroup(cus.Meta["groupName"]) if err == mgo.ErrNotFound { return nil } if err != nil { return err } // here sub count might be 0, but should not be gt 1 if cus.Subs.Count > 1 { return errors.New("customer should only have one subscription") } subID := "" subStatus := SubStatusCanceled // if we dont have any sub, set it as canceled if cus.Subs.Count == 1 { subID = cus.Subs.Values[0].ID subStatus = cus.Subs.Values[0].Status } // if subID and subStatus are same, update not needed if group.Payment.Subscription.ID == subID && stripe.SubStatus(group.Payment.Subscription.Status) == subStatus { return nil } return modelhelper.UpdateGroupPartial( modelhelper.Selector{"_id": group.Id}, modelhelper.Selector{ "$set": modelhelper.Selector{ "payment.subscription.id": subID, "payment.subscription.status": string(subStatus), }, }, ) }
// DeleteCustomerForGroup deletes the customer for a given group. If customer is // not registered, returns error. If customer is already deleted, returns success. // Not necessarily should be used. Call with care. func DeleteCustomerForGroup(groupName string) error { group, err := modelhelper.GetGroup(groupName) if err != nil { return err } if group.Payment.Customer.ID == "" { return nil } if err := deleteCustomer(group.Payment.Customer.ID); err != nil { return err } return modelhelper.UpdateGroupPartial( modelhelper.Selector{"_id": group.Id}, modelhelper.Selector{ // deleting customer deletes everything belong to that customer in stripe, // so say we all "$unset": modelhelper.Selector{"payment": ""}, }, ) }
func (mwc *Controller) migrateAllGroups() { mwc.log.Notice("Group migration started") s := modelhelper.Selector{ "migration": modelhelper.Selector{"$exists": false}, } errCount := 0 successCount := 0 handleError := func(g *mongomodels.Group, err error) { mwc.log.Error("an error occurred for group %s: %s", g.Id.Hex(), err) errCount++ s := modelhelper.Selector{"slug": g.Slug} o := modelhelper.Selector{"$set": modelhelper.Selector{"migration": MigrationFailed, "error": err.Error()}} if err := modelhelper.UpdateGroupPartial(s, o); err != nil { mwc.log.Warning("Could not update group document: %s", err) } } migrateGroup := func(group interface{}) error { oldGroup := group.(*mongomodels.Group) if oldGroup.SocialApiChannelId != "" { s := modelhelper.Selector{"slug": oldGroup.Slug} o := modelhelper.Selector{"$set": modelhelper.Selector{"migration": MigrationCompleted}} modelhelper.UpdateGroupPartial(s, o) return nil } c, err := mwc.createGroupChannel(oldGroup.Slug) if err != nil { handleError(oldGroup, err) return nil } // if err := mwc.createGroupMembers(&group, c.Id); err != nil { // handleError(&group, err) // continue // return nil // } if err := completeGroupMigration(oldGroup, c.Id); err != nil { handleError(oldGroup, err) return nil } successCount++ return nil } iterOptions := helpers.NewIterOptions() iterOptions.CollectionName = "jGroups" iterOptions.F = migrateGroup iterOptions.Filter = s iterOptions.Result = &mongomodels.Group{} iterOptions.Limit = 10000000 iterOptions.Skip = 0 helpers.Iter(modelhelper.Mongo, iterOptions) helpers.Iter(modelhelper.Mongo, iterOptions) mwc.log.Notice("Group migration completed for %d groups with %d errors", successCount, errCount) }