<commit_before>package helpers

import (
	"time"

	"github.com/pborman/uuid"

	"github.com/ghmeier/bloodlines/gateways"
	"github.com/ghmeier/bloodlines/models"
	tg "github.com/jakelong95/TownCenter/gateways"
)

/*ReceiptI describes methods for the Receipt Helper*/
type ReceiptI interface {
	GetAll(int, int) ([]*models.Receipt, error)
	GetByID(string) (*models.Receipt, error)
	Insert(*models.Receipt) error
	SetStatus(uuid.UUID, models.Status) error
	Send(*models.SendRequest) error
	//Consume() error
	DeliverContent(*models.Receipt, *models.Content) error
}

/*Receipt helps with managing receipt entities and fetching them*/
type Receipt struct {
	*baseHelper
	SG gateways.SendgridI
	TC tg.TownCenterI
	RB gateways.RabbitI
}

/*NewReceipt constructs and returns a receipt helper*/
func NewReceipt(sql gateways.SQL, sendgrid gateways.SendgridI, townCenter tg.TownCenterI, rabbit gateways.RabbitI) ReceiptI {
	helper := &Receipt{
		baseHelper: &baseHelper{sql: sql},
		SG:         sendgrid,
		TC:         townCenter,
		RB:         rabbit,
	}
	return helper
}

/*Insert takes a receipt model and appends it to the entity*/
func (r *Receipt) Insert(receipt *models.Receipt) error {
	err := r.sql.Modify(
		"INSERT INTO receipt (id, ts, vals, sendState, contentId, userId) VALUES (?, ?, ?, ?, ?, ?)",
		receipt.ID,
		receipt.Created,
		models.SerializeValues(receipt.Values),
		string(receipt.SendState),
		receipt.ContentID,
		receipt.UserID,
	)
	return err
}

/*GetAll returns a list of receipts of length <limit> starting at <offset>*/
func (r *Receipt) GetAll(offset int, limit int) ([]*models.Receipt, error) {
	rows, err := r.sql.Select("SELECT id, ts, vals, sendState, contentId, userId, finished FROM receipt ORDER BY ts DESC LIMIT ?,? ", offset, limit)
	if err != nil {
		return nil, err
	}

	receipts, err := models.ReceiptFromSQL(rows)
	if err != nil {
		return nil, err
	}

	return receipts, nil
}

/*GetByID returns the receipt entitiy with the given id*/
func (r *Receipt) GetByID(id string) (*models.Receipt, error) {
	rows, err := r.sql.Select("SELECT id, ts, vals, sendState, contentId, userId, finished FROM receipt WHERE id=?", id)
	if err != nil {
		return nil, err
	}

	receipts, err := models.ReceiptFromSQL(rows)
	if err != nil {
		return nil, err
	}
	return receipts[0], nil
}

/*SetStatus updates the status of the receipt with the given id*/
func (r *Receipt) SetStatus(id uuid.UUID, state models.Status) error {
	var err error
	if state == models.SUCCESS {
		err = r.sql.Modify("UPDATE receipt SET sendState=?, finished=? where id=?", string(state), time.Now(), id)
	} else {
		err = r.sql.Modify("UPDATE receipt SET sendState=? where id=?", string(state), id)
	}

	return err
}

/*Send attempts to send the text to the recipient in the receipt*/
//receipt *models.Receipt, subject string, text string
func (r *Receipt) Send(request *models.SendRequest) error {
	err := r.RB.Produce(request)
	if err != nil {
		r.SetStatus(request.ReceiptID, models.FAILURE)
		return err
	}

	err = r.SetStatus(request.ReceiptID, models.QUEUED)
	return err
}

/*DeliverContent combines receipt and content to send a message to the given ContentType */
func (r *Receipt) DeliverContent(receipt *models.Receipt, content *models.Content) error {
	//ignoring error until TC is actually implemented
	switch content.Type {
	case models.EMAIL:
		return r.deliverEmail(receipt, content)
	default:
		return r.deliverNoop(receipt, content)<commit_msg>Cast string to status<commit_after>package helpers

import (
	"time"

	"github.com/pborman/uuid"

	"github.com/ghmeier/bloodlines/gateways"
	"github.com/ghmeier/bloodlines/models"
	tg "github.com/jakelong95/TownCenter/gateways"
)

/*ReceiptI describes methods for the Receipt Helper*/
type ReceiptI interface {
	GetAll(int, int) ([]*models.Receipt, error)
	GetByID(string) (*models.Receipt, error)
	Insert(*models.Receipt) error
	SetStatus(uuid.UUID, models.Status) error
	Send(*models.SendRequest) error
	//Consume() error
	DeliverContent(*models.Receipt, *models.Content) error
}

/*Receipt helps with managing receipt entities and fetching them*/
type Receipt struct {
	*baseHelper
	SG gateways.SendgridI
	TC tg.TownCenterI
	RB gateways.RabbitI
}

/*NewReceipt constructs and returns a receipt helper*/
func NewReceipt(sql gateways.SQL, sendgrid gateways.SendgridI, townCenter tg.TownCenterI, rabbit gateways.RabbitI) ReceiptI {
	helper := &Receipt{
		baseHelper: &baseHelper{sql: sql},
		SG:         sendgrid,
		TC:         townCenter,
		RB:         rabbit,
	}
	return helper
}

/*Insert takes a receipt model and appends it to the entity*/
func (r *Receipt) Insert(receipt *models.Receipt) error {
	err := r.sql.Modify(
		"INSERT INTO receipt (id, ts, vals, sendState, contentId, userId) VALUES (?, ?, ?, ?, ?, ?)",
		receipt.ID,
		receipt.Created,
		models.SerializeValues(receipt.Values),
		string(receipt.SendState),
		receipt.ContentID,
		receipt.UserID,
	)
	return err
}

/*GetAll returns a list of receipts of length <limit> starting at <offset>*/
func (r *Receipt) GetAll(offset int, limit int) ([]*models.Receipt, error) {
	rows, err := r.sql.Select("SELECT id, ts, vals, sendState, contentId, userId, finished FROM receipt ORDER BY ts DESC LIMIT ?,? ", offset, limit)
	if err != nil {
		return nil, err
	}

	receipts, err := models.ReceiptFromSQL(rows)
	if err != nil {
		return nil, err
	}

	return receipts, nil
}

/*GetByID returns the receipt entitiy with the given id*/
func (r *Receipt) GetByID(id string) (*models.Receipt, error) {
	rows, err := r.sql.Select("SELECT id, ts, vals, sendState, contentId, userId, finished FROM receipt WHERE id=?", id)
	if err != nil {
		return nil, err
	}

	receipts, err := models.ReceiptFromSQL(rows)
	if err != nil {
		return nil, err
	}
	return receipts[0], nil
}

/*SetStatus updates the status of the receipt with the given id*/
func (r *Receipt) SetStatus(id uuid.UUID, state models.Status) error {
	var err error
	if state == models.Status(models.SUCCESS) {
		err = r.sql.Modify("UPDATE receipt SET sendState=?, finished=? where id=?", string(state), time.Now(), id)
	} else {
		err = r.sql.Modify("UPDATE receipt SET sendState=? where id=?", string(state), id)
	}

	return err
}

/*Send attempts to send the text to the recipient in the receipt*/
//receipt *models.Receipt, subject string, text string
func (r *Receipt) Send(request *models.SendRequest) error {
	err := r.RB.Produce(request)
	if err != nil {
		r.SetStatus(request.ReceiptID, models.FAILURE)
		return err
	}

	err = r.SetStatus(request.ReceiptID, models.QUEUED)
	return err
}

/*DeliverContent combines receipt and content to send a message to the given ContentType */
func (r *Receipt) DeliverContent(receipt *models.Receipt, content *models.Content) error {
	//ignoring error until TC is actually implemented
	switch content.Type {
	case models.EMAIL:
		return r.deliverEmail(receipt, content)
	default:
		return r.deliverNoop(receipt, content)
{'fraction_non_alphanumeric': 0.1220216606498195, 'fraction_numerical': 0.0008664259927797834, 'mean_word_length': 4.348262548262548, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4288781', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2331, 'n_words': 714}