Add meeting and meeting summary objects
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
committed by
Bryan Frimin
parent
0a9033aaaf
commit
139f5984e2
@@ -66,4 +66,5 @@ const (
|
||||
SAMLConfigurationEntityType uint16 = 42
|
||||
UserAPIKeyEntityType uint16 = 43
|
||||
UserAPIKeyMembershipEntityType uint16 = 44
|
||||
MeetingEntityType uint16 = 45
|
||||
)
|
||||
|
||||
307
pkg/coredata/meeting.go
Normal file
307
pkg/coredata/meeting.go
Normal file
@@ -0,0 +1,307 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type (
|
||||
Meeting struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Name string `db:"name"`
|
||||
Date time.Time `db:"date"`
|
||||
Minutes *string `db:"minutes"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
Meetings []*Meeting
|
||||
|
||||
ErrMeetingNotFound struct {
|
||||
Identifier string
|
||||
}
|
||||
|
||||
ErrMeetingAlreadyExists struct {
|
||||
message string
|
||||
}
|
||||
)
|
||||
|
||||
func (e ErrMeetingNotFound) Error() string {
|
||||
return fmt.Sprintf("meeting not found: %s", e.Identifier)
|
||||
}
|
||||
|
||||
func (e ErrMeetingAlreadyExists) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (m Meeting) CursorKey(orderBy MeetingOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case MeetingOrderFieldCreatedAt:
|
||||
return page.NewCursorKey(m.ID, m.CreatedAt)
|
||||
case MeetingOrderFieldDate:
|
||||
return page.NewCursorKey(m.ID, m.Date)
|
||||
case MeetingOrderFieldName:
|
||||
return page.NewCursorKey(m.ID, m.Name)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
}
|
||||
|
||||
func (m *Meeting) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
meetingID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
name,
|
||||
date,
|
||||
minutes,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
meetings
|
||||
WHERE
|
||||
%s
|
||||
AND id = @meeting_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"meeting_id": meetingID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query meetings: %w", err)
|
||||
}
|
||||
|
||||
meeting, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Meeting])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return &ErrMeetingNotFound{Identifier: meetingID.String()}
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect meeting: %w", err)
|
||||
}
|
||||
|
||||
*m = meeting
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Meetings) LoadByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[MeetingOrderField],
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
name,
|
||||
date,
|
||||
minutes,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
meetings
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
AND %s
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query meetings: %w", err)
|
||||
}
|
||||
|
||||
meetings, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Meeting])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect meetings: %w", err)
|
||||
}
|
||||
|
||||
*m = meetings
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Meetings) CountByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
) (int, error) {
|
||||
q := `
|
||||
SELECT
|
||||
COUNT(*)
|
||||
FROM
|
||||
meetings
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"organization_id": organizationID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
row := conn.QueryRow(ctx, q, args)
|
||||
var count int
|
||||
if err := row.Scan(&count); err != nil {
|
||||
return 0, fmt.Errorf("cannot count meetings: %w", err)
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (m *Meeting) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO
|
||||
meetings (
|
||||
tenant_id,
|
||||
id,
|
||||
organization_id,
|
||||
name,
|
||||
date,
|
||||
minutes,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
@tenant_id,
|
||||
@meeting_id,
|
||||
@organization_id,
|
||||
@name,
|
||||
@date,
|
||||
@minutes,
|
||||
@created_at,
|
||||
@updated_at
|
||||
);
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"meeting_id": m.ID,
|
||||
"organization_id": m.OrganizationID,
|
||||
"name": m.Name,
|
||||
"date": m.Date,
|
||||
"minutes": m.Minutes,
|
||||
"created_at": m.CreatedAt,
|
||||
"updated_at": m.UpdatedAt,
|
||||
}
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert meeting: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Meeting) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE meetings
|
||||
SET
|
||||
name = @name,
|
||||
date = @date,
|
||||
minutes = @minutes,
|
||||
updated_at = @updated_at
|
||||
WHERE %s
|
||||
AND id = @meeting_id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"meeting_id": m.ID,
|
||||
"name": m.Name,
|
||||
"date": m.Date,
|
||||
"minutes": m.Minutes,
|
||||
"updated_at": m.UpdatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
result, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update meeting: %w", err)
|
||||
}
|
||||
|
||||
if result.RowsAffected() == 0 {
|
||||
return &ErrMeetingNotFound{Identifier: m.ID.String()}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Meeting) Delete(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
DELETE FROM meetings
|
||||
WHERE %s
|
||||
AND id = @meeting_id
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"meeting_id": m.ID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
result, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete meeting: %w", err)
|
||||
}
|
||||
|
||||
if result.RowsAffected() == 0 {
|
||||
return &ErrMeetingNotFound{Identifier: m.ID.String()}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
123
pkg/coredata/meeting_attendee.go
Normal file
123
pkg/coredata/meeting_attendee.go
Normal file
@@ -0,0 +1,123 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
)
|
||||
|
||||
type (
|
||||
MeetingAttendee struct {
|
||||
MeetingID gid.GID `db:"meeting_id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
AttendeeID gid.GID `db:"attendee_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
MeetingAttendees []*MeetingAttendee
|
||||
)
|
||||
|
||||
func (ma *MeetingAttendees) LoadByMeetingID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
meetingID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
meeting_id,
|
||||
attendee_id,
|
||||
organization_id,
|
||||
created_at
|
||||
FROM
|
||||
meeting_attendees
|
||||
WHERE
|
||||
%s
|
||||
AND meeting_id = @meeting_id
|
||||
ORDER BY
|
||||
created_at ASC
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"meeting_id": meetingID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query meeting attendees: %w", err)
|
||||
}
|
||||
|
||||
attendees, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[MeetingAttendee])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect meeting attendees: %w", err)
|
||||
}
|
||||
|
||||
*ma = attendees
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ma *MeetingAttendees) Merge(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
meetingID gid.GID,
|
||||
organizationID gid.GID,
|
||||
attendeeIDs []gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
WITH attendee_ids AS (
|
||||
SELECT
|
||||
unnest(@attendee_ids::text[]) AS attendee_id,
|
||||
@tenant_id AS tenant_id,
|
||||
@meeting_id AS meeting_id,
|
||||
@organization_id AS organization_id,
|
||||
@created_at::timestamptz AS created_at
|
||||
)
|
||||
MERGE INTO meeting_attendees AS tgt
|
||||
USING attendee_ids AS src
|
||||
ON tgt.tenant_id = src.tenant_id
|
||||
AND tgt.meeting_id = src.meeting_id
|
||||
AND tgt.attendee_id = src.attendee_id
|
||||
WHEN NOT MATCHED THEN
|
||||
INSERT (tenant_id, meeting_id, attendee_id, organization_id, created_at)
|
||||
VALUES (src.tenant_id, src.meeting_id, src.attendee_id, src.organization_id, src.created_at)
|
||||
WHEN NOT MATCHED BY SOURCE
|
||||
AND tgt.tenant_id = @tenant_id AND tgt.meeting_id = @meeting_id
|
||||
THEN DELETE
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"meeting_id": meetingID,
|
||||
"organization_id": organizationID,
|
||||
"created_at": time.Now(),
|
||||
"attendee_ids": attendeeIDs,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot merge meeting attendees: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
57
pkg/coredata/meeting_order_field.go
Normal file
57
pkg/coredata/meeting_order_field.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
MeetingOrderField string
|
||||
)
|
||||
|
||||
const (
|
||||
MeetingOrderFieldDate MeetingOrderField = "DATE"
|
||||
MeetingOrderFieldName MeetingOrderField = "NAME"
|
||||
MeetingOrderFieldCreatedAt MeetingOrderField = "CREATED_AT"
|
||||
)
|
||||
|
||||
func (p MeetingOrderField) Column() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p MeetingOrderField) String() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p MeetingOrderField) IsValid() bool {
|
||||
switch p {
|
||||
case MeetingOrderFieldDate, MeetingOrderFieldName, MeetingOrderFieldCreatedAt:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p MeetingOrderField) MarshalText() ([]byte, error) {
|
||||
return []byte(p.String()), nil
|
||||
}
|
||||
|
||||
func (p *MeetingOrderField) UnmarshalText(text []byte) error {
|
||||
*p = MeetingOrderField(text)
|
||||
if !p.IsValid() {
|
||||
return fmt.Errorf("%s is not a valid MeetingOrderField", string(text))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
32
pkg/coredata/migrations/20251109T101900Z.sql
Normal file
32
pkg/coredata/migrations/20251109T101900Z.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
CREATE TABLE meetings (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
organization_id TEXT NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
minutes TEXT,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE meeting_attendees (
|
||||
meeting_id TEXT NOT NULL REFERENCES meetings(id) ON DELETE CASCADE,
|
||||
attendee_id TEXT NOT NULL REFERENCES peoples(id) ON DELETE CASCADE,
|
||||
organization_id TEXT NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
tenant_id TEXT NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
PRIMARY KEY (meeting_id, attendee_id)
|
||||
);
|
||||
|
||||
CREATE TABLE organization_contexts (
|
||||
organization_id TEXT PRIMARY KEY REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
tenant_id TEXT NOT NULL,
|
||||
summary TEXT,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO organization_contexts (organization_id, tenant_id, summary, created_at, updated_at)
|
||||
SELECT id AS organization_id, tenant_id, NULL AS summary, NOW() AS created_at, NOW() AS updated_at
|
||||
FROM organizations
|
||||
ON CONFLICT (organization_id) DO NOTHING;
|
||||
162
pkg/coredata/organization_context.go
Normal file
162
pkg/coredata/organization_context.go
Normal file
@@ -0,0 +1,162 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
)
|
||||
|
||||
type (
|
||||
OrganizationContext struct {
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Summary *string `db:"summary"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
ErrOrganizationContextNotFound struct {
|
||||
Identifier string
|
||||
}
|
||||
)
|
||||
|
||||
func (e ErrOrganizationContextNotFound) Error() string {
|
||||
return fmt.Sprintf("organization context not found: %q", e.Identifier)
|
||||
}
|
||||
|
||||
func (oc *OrganizationContext) LoadByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
organization_id,
|
||||
summary,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
organization_contexts
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query organization context: %w", err)
|
||||
}
|
||||
|
||||
orgContext, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[OrganizationContext])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return &ErrOrganizationContextNotFound{Identifier: organizationID.String()}
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect organization context: %w", err)
|
||||
}
|
||||
|
||||
*oc = orgContext
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (oc *OrganizationContext) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO organization_contexts (
|
||||
organization_id,
|
||||
tenant_id,
|
||||
summary,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
@organization_id,
|
||||
@tenant_id,
|
||||
@summary,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"organization_id": oc.OrganizationID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"summary": oc.Summary,
|
||||
"created_at": oc.CreatedAt,
|
||||
"updated_at": oc.UpdatedAt,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert organization context: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (oc *OrganizationContext) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE organization_contexts
|
||||
SET
|
||||
summary = @summary,
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"organization_id": oc.OrganizationID,
|
||||
"summary": oc.Summary,
|
||||
"updated_at": oc.UpdatedAt,
|
||||
}
|
||||
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
result, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update organization context: %w", err)
|
||||
}
|
||||
|
||||
if result.RowsAffected() == 0 {
|
||||
return &ErrOrganizationContextNotFound{Identifier: oc.OrganizationID.String()}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -21,10 +21,10 @@ import (
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -176,6 +176,52 @@ func (p *People) LoadByEmail(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Peoples) LoadByIDs(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
peopleIDs []gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
kind,
|
||||
full_name,
|
||||
primary_email_address,
|
||||
additional_email_addresses,
|
||||
position,
|
||||
contract_start_date,
|
||||
contract_end_date,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
peoples
|
||||
WHERE
|
||||
%s
|
||||
AND id = ANY(@people_ids)
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"people_ids": peopleIDs}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query people: %w", err)
|
||||
}
|
||||
|
||||
peoples, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[People])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect people: %w", err)
|
||||
}
|
||||
|
||||
*p = peoples
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p People) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
@@ -427,3 +473,72 @@ INNER JOIN signatories ON peoples.id = signatories.signed_by
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Peoples) LoadByMeetingID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
meetingID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
WITH people_attendees AS (
|
||||
SELECT
|
||||
p.id,
|
||||
p.organization_id,
|
||||
p.kind,
|
||||
p.full_name,
|
||||
p.primary_email_address,
|
||||
p.additional_email_addresses,
|
||||
p.position,
|
||||
p.contract_start_date,
|
||||
p.contract_end_date,
|
||||
p.created_at,
|
||||
p.updated_at,
|
||||
p.tenant_id,
|
||||
ma.created_at AS attendee_created_at
|
||||
FROM
|
||||
peoples p
|
||||
INNER JOIN
|
||||
meeting_attendees ma ON p.id = ma.attendee_id
|
||||
WHERE
|
||||
ma.meeting_id = @meeting_id
|
||||
)
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
kind,
|
||||
full_name,
|
||||
primary_email_address,
|
||||
additional_email_addresses,
|
||||
position,
|
||||
contract_start_date,
|
||||
contract_end_date,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
people_attendees
|
||||
WHERE
|
||||
%s
|
||||
ORDER BY
|
||||
attendee_created_at ASC
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"meeting_id": meetingID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query people: %w", err)
|
||||
}
|
||||
|
||||
peoples, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[People])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect people: %w", err)
|
||||
}
|
||||
|
||||
*p = peoples
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user