57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package services
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"adam-french.co.uk/job-tracker/internal/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ApplicationEventInput is deliberately small: application changes supply
|
|
// their own source, while a manual event can include a note and contact data.
|
|
type ApplicationEventInput struct {
|
|
Kind string
|
|
OccurredAt time.Time
|
|
Note *string
|
|
Source string
|
|
ContactName *string
|
|
ContactEmail *string
|
|
ContactRole *string
|
|
}
|
|
|
|
// RecordApplicationEvent appends an event without changing the application.
|
|
func RecordApplicationEvent(db *gorm.DB, applicationID uint, input ApplicationEventInput) (*models.JobApplicationEvent, error) {
|
|
if input.OccurredAt.IsZero() {
|
|
input.OccurredAt = time.Now()
|
|
}
|
|
if strings.TrimSpace(input.Kind) == "" {
|
|
return nil, &JobInputError{"event kind is required"}
|
|
}
|
|
if input.Source == "" {
|
|
input.Source = "manual"
|
|
}
|
|
event := &models.JobApplicationEvent{
|
|
JobApplicationID: applicationID,
|
|
Kind: strings.TrimSpace(input.Kind),
|
|
OccurredAt: input.OccurredAt,
|
|
Note: input.Note,
|
|
Source: input.Source,
|
|
ContactName: input.ContactName,
|
|
ContactEmail: input.ContactEmail,
|
|
ContactRole: input.ContactRole,
|
|
}
|
|
if err := db.Create(event).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return event, nil
|
|
}
|
|
|
|
// ListApplicationEvents returns newest activity first, with id as a stable tie-break.
|
|
func ListApplicationEvents(db *gorm.DB, applicationID uint) ([]*models.JobApplicationEvent, error) {
|
|
var events []*models.JobApplicationEvent
|
|
err := db.Where("job_application_id = ?", applicationID).
|
|
Order("occurred_at DESC, id DESC").Find(&events).Error
|
|
return events, err
|
|
}
|