-
Notifications
You must be signed in to change notification settings - Fork 70
/
org.go
133 lines (116 loc) · 4.55 KB
/
org.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package diygoapi
import (
"context"
"github.com/gilcrest/diygoapi/errs"
"github.com/gilcrest/diygoapi/secure"
"github.com/gilcrest/diygoapi/uuid"
)
// OrgServicer manages the retrieval and manipulation of an Org
type OrgServicer interface {
// Create manages the creation of an Org (and optional app)
Create(ctx context.Context, r *CreateOrgRequest, adt Audit) (*OrgResponse, error)
Update(ctx context.Context, r *UpdateOrgRequest, adt Audit) (*OrgResponse, error)
Delete(ctx context.Context, extlID string) (DeleteResponse, error)
FindAll(ctx context.Context) ([]*OrgResponse, error)
FindByExternalID(ctx context.Context, extlID string) (*OrgResponse, error)
}
// OrgKind is a way of classifying an organization. Examples are Genesis, Test, Standard
type OrgKind struct {
// ID: The unique identifier
ID uuid.UUID
// External ID: The unique external identifier
ExternalID string
// Description: A longer description of the organization kind
Description string
}
// Validate determines whether the Person has proper data to be considered valid
func (o OrgKind) Validate() error {
const op errs.Op = "diygoapi/OrgKind.Validate"
switch {
case o.ID == uuid.Nil:
return errs.E(op, errs.Validation, "OrgKind ID cannot be nil")
case o.ExternalID == "":
return errs.E(op, errs.Validation, "OrgKind ExternalID cannot be empty")
case o.Description == "":
return errs.E(op, errs.Validation, "OrgKind Description cannot be empty")
}
return nil
}
// Org represents an Organization (company, institution or any other
// organized body of people with a particular purpose)
type Org struct {
// ID: The unique identifier
ID uuid.UUID
// External ID: The unique external identifier
ExternalID secure.Identifier
// Name: The organization name
Name string
// Description: A longer description of the organization
Description string
// Kind: a way of classifying organizations
Kind *OrgKind
}
// Validate determines whether the Org has proper data to be considered valid
func (o Org) Validate() (err error) {
const op errs.Op = "diygoapi/Org.Validate"
switch {
case o.ID == uuid.Nil:
return errs.E(op, errs.Validation, "Org ID cannot be nil")
case o.ExternalID.String() == "":
return errs.E(op, errs.Validation, "Org ExternalID cannot be empty")
case o.Name == "":
return errs.E(op, errs.Validation, "Org Name cannot be empty")
case o.Description == "":
return errs.E(op, errs.Validation, "Org Description cannot be empty")
}
if err = o.Kind.Validate(); err != nil {
return errs.E(op, err)
}
return nil
}
// CreateOrgRequest is the request struct for Creating an Org
type CreateOrgRequest struct {
Name string `json:"name"`
Description string `json:"description"`
Kind string `json:"kind"`
CreateAppRequest *CreateAppRequest `json:"app"`
}
// Validate determines whether the CreateOrgRequest has proper data to be considered valid
func (r CreateOrgRequest) Validate() error {
const op errs.Op = "diygoapi/CreateOrgRequest.Validate"
switch {
case r.Name == "":
return errs.E(op, errs.Validation, "org name is required")
case r.Description == "":
return errs.E(op, errs.Validation, "org description is required")
case r.Kind == "":
return errs.E(op, errs.Validation, "org kind is required")
}
return nil
}
// UpdateOrgRequest is the request struct for Updating an Org
type UpdateOrgRequest struct {
ExternalID string
Name string `json:"name"`
Description string `json:"description"`
}
// OrgResponse is the response struct for an Org.
// It contains only one app (even though an org can have many apps).
// This app is only present in the response when creating an org and
// accompanying app. I may change this later to be different response
// structs for different purposes, but for now, this works.
type OrgResponse struct {
ExternalID string `json:"external_id"`
Name string `json:"name"`
KindExternalID string `json:"kind_description"`
Description string `json:"description"`
CreateAppExtlID string `json:"create_app_extl_id"`
CreateUserFirstName string `json:"create_user_first_name"`
CreateUserLastName string `json:"create_user_last_name"`
CreateDateTime string `json:"create_date_time"`
UpdateAppExtlID string `json:"update_app_extl_id"`
UpdateUserFirstName string `json:"update_user_first_name"`
UpdateUserLastName string `json:"update_user_last_name"`
UpdateDateTime string `json:"update_date_time"`
App *AppResponse `json:"app,omitempty"`
}