Beispiel #1
1
func (a *Account) SendPrompt() error {
	// Pull info from the environment vars.
	err := queries.SendSms(a.PhoneNumber, "How have things been since your last checkin? Reply 'good', 'bad', or 'ugly'.")
	// If there's an error, give them some context.
	if err != nil {
		return errors.New("Sending text to " + a.PhoneNumber + "resulted in error: " + err.Error())
	} else {
		return nil
	}
}
Beispiel #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
}