aboutsummaryrefslogtreecommitdiff
path: root/cmd/mi/models/switch.go
blob: 5a16dd62b349ef3d4d27b46283027f5886809241 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package models

import (
	"time"

	"gorm.io/gorm"
	pb "within.website/x/proto/mi"
)

// Switch is a record of the system switching front to a different member.
type Switch struct {
	gorm.Model            // adds CreatedAt, UpdatedAt, DeletedAt
	ID         string     `gorm:"uniqueIndex"` // unique identifier for the switch (ULID usually)
	EndedAt    *time.Time // when the switch ends, different from DeletedAt
	MemberID   int        // the member who is now in front
	Member     Member     `gorm:"foreignKey:MemberID"`
}

// AsProto converts a Switch to its protobuf representation.
func (s *Switch) AsProto() *pb.Switch {
	if s == nil {
		return nil
	}

	var endedAt string

	if s.EndedAt != nil {
		endedAt = s.EndedAt.Format(time.RFC3339)
	}

	return &pb.Switch{
		Id:        s.ID,
		StartedAt: s.CreatedAt.Format(time.RFC3339),
		EndedAt:   endedAt,
		MemberId:  int32(s.MemberID),
	}
}

// AsFrontChange converts a Switch to a FrontChange protobuf.
func (s Switch) AsFrontChange() *pb.FrontChange {
	return &pb.FrontChange{
		Member: s.Member.AsProto(),
		Switch: s.AsProto(),
	}
}