Example #1
1
func (c *Checkin) PushToAccount(phoneNumber string) error {
	return queries.UpdateAccount(bson.M{
		"phoneNumber":                phoneNumber,
		"archivedCheckins.twilioSid": bson.M{"$ne": c.TwilioSid},
		"newCheckins.twilioSid":      bson.M{"$ne": c.TwilioSid},
	}, bson.M{
		"$push": bson.M{"newCheckins": c},
	})
}
Example #2
1
func NotifyPartnersOfNewCheckins() (numNotificationsSent int, err error) {
	var accountsWithNewCheckins *[]models.Account
	// Which accounts need to have notifications sent?
	accountsWithNewCheckins, err = models.ListAccountsWithNewCheckins()
	if err != nil {
		return
	}

	// Loop through each and send notifications.
	for _, a := range *accountsWithNewCheckins {
		// Of all the new checkins, find the most recent.
		mostRecentCheckin := new(models.Checkin)
		for _, newCheckin := range a.NewCheckins {
			if mostRecentCheckin == nil || newCheckin.ReceivedAt.After(mostRecentCheckin.ReceivedAt) {
				*mostRecentCheckin = newCheckin
			}
		}

		// Form the notification body.
		body := fmt.Sprintf("%s says things have been %s.", a.Name, mostRecentCheckin.Status)
		// Send it to each partner.
		for _, partnerPhoneNumber := range a.Partners {
			err = queries.SendSms(partnerPhoneNumber, body)
			if err != nil {
				return
			} else {
				numNotificationsSent++
			}
		}

		// Move the relevant checkins to the archived list.
		err = queries.UpdateAccount(
			bson.M{"_id": a.Id},
			bson.M{
				"$push":  bson.M{"archivedCheckins": bson.M{"$each": a.NewCheckins}},
				"$unset": bson.M{"newCheckins": true},
			},
		)
		if err != nil {
			return
		}
	}

	return
}