|
| 1 | +package mysql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/fleetdm/fleet/v4/server/contexts/ctxerr" |
| 10 | + "github.com/fleetdm/fleet/v4/server/fleet" |
| 11 | + "github.com/jmoiron/sqlx" |
| 12 | +) |
| 13 | + |
| 14 | +func (ds *Datastore) NewCalendarEvent( |
| 15 | + ctx context.Context, |
| 16 | + email string, |
| 17 | + startTime time.Time, |
| 18 | + endTime time.Time, |
| 19 | + data []byte, |
| 20 | + hostID uint, |
| 21 | +) (*fleet.CalendarEvent, error) { |
| 22 | + var calendarEvent *fleet.CalendarEvent |
| 23 | + if err := ds.withRetryTxx(ctx, func(tx sqlx.ExtContext) error { |
| 24 | + const calendarEventsQuery = ` |
| 25 | + INSERT INTO calendar_events ( |
| 26 | + email, |
| 27 | + start_time, |
| 28 | + end_time, |
| 29 | + event |
| 30 | + ) VALUES (?, ?, ?, ?); |
| 31 | + ` |
| 32 | + result, err := tx.ExecContext( |
| 33 | + ctx, |
| 34 | + calendarEventsQuery, |
| 35 | + email, |
| 36 | + startTime, |
| 37 | + endTime, |
| 38 | + data, |
| 39 | + ) |
| 40 | + if err != nil { |
| 41 | + return ctxerr.Wrap(ctx, err, "insert calendar event") |
| 42 | + } |
| 43 | + |
| 44 | + id, _ := result.LastInsertId() |
| 45 | + calendarEvent = &fleet.CalendarEvent{ |
| 46 | + ID: uint(id), |
| 47 | + Email: email, |
| 48 | + StartTime: startTime, |
| 49 | + EndTime: endTime, |
| 50 | + Data: data, |
| 51 | + } |
| 52 | + |
| 53 | + const hostCalendarEventsQuery = ` |
| 54 | + INSERT INTO host_calendar_events ( |
| 55 | + host_id, |
| 56 | + calendar_event_id, |
| 57 | + webhook_status |
| 58 | + ) VALUES (?, ?, ?); |
| 59 | + ` |
| 60 | + result, err = tx.ExecContext( |
| 61 | + ctx, |
| 62 | + hostCalendarEventsQuery, |
| 63 | + hostID, |
| 64 | + calendarEvent.ID, |
| 65 | + fleet.CalendarWebhookStatusPending, |
| 66 | + ) |
| 67 | + if err != nil { |
| 68 | + return ctxerr.Wrap(ctx, err, "insert host calendar event") |
| 69 | + } |
| 70 | + return nil |
| 71 | + }); err != nil { |
| 72 | + return nil, ctxerr.Wrap(ctx, err) |
| 73 | + } |
| 74 | + return calendarEvent, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (ds *Datastore) GetCalendarEvent(ctx context.Context, email string) (*fleet.CalendarEvent, error) { |
| 78 | + const calendarEventsQuery = ` |
| 79 | + SELECT * FROM calendar_events WHERE email = ?; |
| 80 | + ` |
| 81 | + var calendarEvent fleet.CalendarEvent |
| 82 | + err := sqlx.GetContext(ctx, ds.reader(ctx), &calendarEvent, calendarEventsQuery, email) |
| 83 | + if err != nil { |
| 84 | + if err == sql.ErrNoRows { |
| 85 | + return nil, ctxerr.Wrap(ctx, notFound("CalendarEvent").WithMessage(fmt.Sprintf("email: %s", email))) |
| 86 | + } |
| 87 | + return nil, ctxerr.Wrap(ctx, err, "get calendar event") |
| 88 | + } |
| 89 | + return &calendarEvent, nil |
| 90 | +} |
| 91 | + |
| 92 | +func (ds *Datastore) UpdateCalendarEvent(ctx context.Context, calendarEventID uint, startTime time.Time, endTime time.Time, data []byte) error { |
| 93 | + const calendarEventsQuery = ` |
| 94 | + UPDATE calendar_events SET |
| 95 | + start_time = ?, |
| 96 | + end_time = ?, |
| 97 | + event = ? |
| 98 | + WHERE id = ?; |
| 99 | + ` |
| 100 | + if _, err := ds.writer(ctx).ExecContext(ctx, calendarEventsQuery, startTime, endTime, data, calendarEventID); err != nil { |
| 101 | + return ctxerr.Wrap(ctx, err, "update calendar event") |
| 102 | + } |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func (ds *Datastore) DeleteCalendarEvent(ctx context.Context, calendarEventID uint) error { |
| 107 | + const calendarEventsQuery = ` |
| 108 | + DELETE FROM calendar_events WHERE id = ?; |
| 109 | + ` |
| 110 | + if _, err := ds.writer(ctx).ExecContext(ctx, calendarEventsQuery, calendarEventID); err != nil { |
| 111 | + return ctxerr.Wrap(ctx, err, "delete calendar event") |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func (ds *Datastore) GetHostCalendarEvent(ctx context.Context, hostID uint) (*fleet.HostCalendarEvent, *fleet.CalendarEvent, error) { |
| 117 | + const hostCalendarEventsQuery = ` |
| 118 | + SELECT * FROM host_calendar_events WHERE host_id = ? |
| 119 | + ` |
| 120 | + var hostCalendarEvent fleet.HostCalendarEvent |
| 121 | + if err := sqlx.GetContext(ctx, ds.reader(ctx), &hostCalendarEvent, hostCalendarEventsQuery, hostID); err != nil { |
| 122 | + if err == sql.ErrNoRows { |
| 123 | + return nil, nil, ctxerr.Wrap(ctx, notFound("HostCalendarEvent").WithMessage(fmt.Sprintf("host_id: %d", hostID))) |
| 124 | + } |
| 125 | + return nil, nil, ctxerr.Wrap(ctx, err, "get host calendar event") |
| 126 | + } |
| 127 | + const calendarEventsQuery = ` |
| 128 | + SELECT * FROM calendar_events WHERE id = ? |
| 129 | + ` |
| 130 | + var calendarEvent fleet.CalendarEvent |
| 131 | + if err := sqlx.GetContext(ctx, ds.reader(ctx), &calendarEvent, calendarEventsQuery, hostCalendarEvent.CalendarEventID); err != nil { |
| 132 | + if err == sql.ErrNoRows { |
| 133 | + return nil, nil, ctxerr.Wrap(ctx, notFound("CalendarEvent").WithID(hostCalendarEvent.CalendarEventID)) |
| 134 | + } |
| 135 | + return nil, nil, ctxerr.Wrap(ctx, err, "get calendar event") |
| 136 | + } |
| 137 | + return &hostCalendarEvent, &calendarEvent, nil |
| 138 | +} |
| 139 | + |
| 140 | +func (ds *Datastore) UpdateHostCalendarWebhookStatus(ctx context.Context, hostID uint, status fleet.CalendarWebhookStatus) error { |
| 141 | + const calendarEventsQuery = ` |
| 142 | + UPDATE host_calendar_events SET |
| 143 | + webhook_status = ? |
| 144 | + WHERE host_id = ?; |
| 145 | + ` |
| 146 | + if _, err := ds.writer(ctx).ExecContext(ctx, calendarEventsQuery, status, hostID); err != nil { |
| 147 | + return ctxerr.Wrap(ctx, err, "update host calendar event webhook status") |
| 148 | + } |
| 149 | + return nil |
| 150 | +} |
0 commit comments