-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
2263 lines (1737 loc) · 51.7 KB
/
service.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"log"
"net/http"
"strings"
"time"
"github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/customer"
"github.com/amborle/featmap/lexorank"
"github.com/asaskevich/govalidator"
jwt "github.com/dgrijalva/jwt-go"
"github.com/go-chi/jwtauth"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"golang.org/x/crypto/bcrypt"
)
// Service ...
type Service interface {
// Technical
SetConfig(x Configuration)
SetAccountObject(a *Account)
SetMemberObject(m *Member)
SetRepoObject(m Repository)
SetAuth(x *jwtauth.JWTAuth)
SetWorkspaceObject(a *Workspace)
SetSubscriptionObject(x *Subscription)
UpdateLatestActivityNow()
GetConfig() Configuration
GetDBObject() *sqlx.DB
GetRepoObject() Repository
GetMemberObject() *Member
GetAccountObject() *Account
GetWorkspaceObject() *Workspace
GetSubscriptionObject() *Subscription
SendEmail(smtpServer string, smtpPort string, smtpUser string, smtpPass string, from string, recipient string, subject string, body string) error
Register(workspaceName string, name string, email string, password string) (*Workspace, *Account, *Member, error)
Login(email string, password string) (*Account, error)
Token(accountID string) string
DeleteAccount() error
CreateWorkspace(name string) (*Workspace, *Subscription, *Member, error)
GetWorkspace(id string) (*Workspace, error)
GetWorkspaceByContext() *Workspace
GetWorkspaces() []*Workspace
GetAccount(accountID string) (*Account, error)
GetAccountsByWorkspace() []*Account
DeleteWorkspace() error
StripeWebhook(r *http.Request) error
GetSubscriptionPlanSession(plan string, quantity int64) (string, error)
GetSubscriptionByWorkspace(id string) *Subscription
GetSubscriptionsByAccount() []*Subscription
ChangeSubscription(plan string, quantity int64) error
ConfirmEmail(key string) error
UpdateEmail(email string) error
UpdateName(name string) error
ResendEmail() error
SendResetEmail(email string) error
SetPassword(password string, key string) error
GetMember(accountID string, workspaceID string) (*Member, error)
GetMembersByAccount() []*Member
GetMembers() []*Member
GetMembersByWorkspace(id string) []*Member
UpdateMemberLevel(memberID string, level string) (*Member, error)
DeleteMember(memberID string) error
CreateMember(workspaceID string, accountID string, level string) (*Member, error)
Leave() error
ChangeAllowExternalSharing(value bool) error
ChangeGeneralInfo(EUVAT string, externalBillingEmail string) error
GetInvitesByWorkspace() []*Invite
CreateInvite(email string, level string) (*Invite, error)
SendInvitationMail(invitationID string) error
DeleteInvite(invitationID string) error
AcceptInvite(code string) error
GetInvite(code string) (*Invite, error)
GetProjectByExternalLink(link string) (*Project, error)
GetProjectExtendedByExternalLink(link string) (*projectResponse, error)
GetProject(id string) *Project
CreateProjectWithID(id string, title string) (*Project, error)
RenameProject(id string, title string) (*Project, error)
DeleteProject(id string) error
GetProjects() []*Project
UpdateProjectDescription(id string, d string) (*Project, error)
CreateMilestoneWithID(id string, projectID string, title string) (*Milestone, error)
MoveMilestone(id string, index int) (*Milestone, error)
RenameMilestone(id string, title string) (*Milestone, error)
GetMilestonesByProject(id string) []*Milestone
DeleteMilestone(id string) error
UpdateMilestoneDescription(id string, d string) (*Milestone, error)
CloseMilestone(id string) (*Milestone, error)
OpenMilestone(id string) (*Milestone, error)
ChangeColorOnMilestone(id string, color string) (*Milestone, error)
UpdateAnnotationsOnMilestone(id string, names string) (*Milestone, error)
GetWorkflowsByProject(id string) []*Workflow
MoveWorkflow(id string, index int) (*Workflow, error)
CreateWorkflowWithID(id string, projectID string, title string) (*Workflow, error)
RenameWorkflow(id string, title string) (*Workflow, error)
DeleteWorkflow(id string) error
UpdateWorkflowDescription(id string, d string) (*Workflow, error)
ChangeColorOnWorkflow(id string, color string) (*Workflow, error)
CloseWorkflow(id string) (*Workflow, error)
OpenWorkflow(id string) (*Workflow, error)
UpdateAnnotationsOnWorkflow(id string, names string) (*Workflow, error)
CreateSubWorkflowWithID(id string, workflowID string, title string) (*SubWorkflow, error)
MoveSubWorkflow(id string, toWorkflowID string, index int) (*SubWorkflow, error)
GetSubWorkflowsByProject(id string) []*SubWorkflow
RenameSubWorkflow(id string, title string) (*SubWorkflow, error)
DeleteSubWorkflow(id string) error
UpdateSubWorkflowDescription(id string, d string) (*SubWorkflow, error)
ChangeColorOnSubWorkflow(id string, color string) (*SubWorkflow, error)
CloseSubWorkflow(id string) (*SubWorkflow, error)
OpenSubWorkflow(id string) (*SubWorkflow, error)
UpdateAnnotationsOnSubWorkflow(id string, names string) (*SubWorkflow, error)
GetFeaturesByProject(id string) []*Feature
MoveFeature(id string, toMilestoneID string, toSubWorkflowID string, index int) (*Feature, error)
CreateFeatureWithID(id string, subWorkflowID string, milestoneID string, title string) (*Feature, error)
RenameFeature(id string, title string) (*Feature, error)
DeleteFeature(id string) error
UpdateFeatureDescription(id string, d string) (*Feature, error)
CloseFeature(id string) (*Feature, error)
OpenFeature(id string) (*Feature, error)
ChangeColorOnFeature(id string, color string) (*Feature, error)
UpdateAnnotationsOnFeature(id string, names string) (*Feature, error)
UpdateEstimateOnFeature(id string, estimate int) (*Feature, error)
GetFeatureCommentsByProject(id string) []*FeatureComment
CreateFeatureCommentWithID(id string, featureID string, post string) (*FeatureComment, error)
UpdateFeatureCommentPost(id string, post string) (*FeatureComment, error)
DeleteFeatureComment(id string) error
GetPersonasByProject(id string) []*Persona
GetWorkflowPersonasByProject(id string) []*WorkflowPersona
CreateWorkflowPersonaWithID(id string, workflowID string, personaID string) (*WorkflowPersona, error)
DeleteWorkflowPersona(id string) error
CreatePersonaWithID(id string, projectID string, avatar string, name string, role string, description string, workflowID string, workflowPersonaID string) (*Persona, error)
DeletePersona(id string) error
UpdatePersona(id string, avatar string, name string, role string, description string) (*Persona, error)
}
type service struct {
config Configuration
Acc *Account
Member *Member
Subscription *Subscription
r Repository
auth *jwtauth.JWTAuth
ws *Workspace
}
// NewFeatmapService ...
func NewFeatmapService() Service {
return &service{}
}
func (s *service) SetConfig(x Configuration) { s.config = x }
func (s *service) SetAccountObject(a *Account) { s.Acc = a }
func (s *service) SetMemberObject(m *Member) { s.Member = m }
func (s *service) SetRepoObject(m Repository) { s.r = m }
func (s *service) SetAuth(x *jwtauth.JWTAuth) { s.auth = x }
func (s *service) SetWorkspaceObject(a *Workspace) { s.ws = a }
func (s *service) SetSubscriptionObject(x *Subscription) { s.Subscription = x }
func (s *service) GetConfig() Configuration { return s.config }
func (s *service) GetDBObject() *sqlx.DB { return s.r.DB() }
func (s *service) GetRepoObject() Repository { return s.r }
func (s *service) GetAccountObject() *Account { return s.Acc }
func (s *service) GetSubscriptionObject() *Subscription { return s.Subscription }
func (s *service) GetMemberObject() *Member { return s.Member }
func (s *service) GetWorkspaceObject() *Workspace { return s.ws }
func (s *service) UpdateLatestActivityNow() {
acc := s.GetAccountObject()
acc.LatestActivity = time.Now().UTC()
s.r.StoreAccount(acc)
}
func (s *service) Register(workspaceName string, name string, email string, password string) (*Workspace, *Account, *Member, error) {
workspaceName = govalidator.Trim(workspaceName, "")
name = govalidator.Trim(name, "")
email = strings.ToLower(govalidator.Trim(email, ""))
if !govalidator.IsEmail(email) {
return nil, nil, nil, errors.New("email_invalid")
}
if s.config.Environment == "test" && name != "test" {
return nil, nil, nil, errors.New("workspace_invalid")
}
if !workspaceNameIsValid(workspaceName) {
return nil, nil, nil, errors.New("workspace_invalid")
}
if len(name) < 1 || len(name) > 200 {
return nil, nil, nil, errors.New("name_invalid")
}
if len(password) < 6 || len(password) > 200 {
return nil, nil, nil, errors.New("password_invalid")
}
// First check if email is not already taken!
dupacc, err := s.r.GetAccountByEmail(email)
if dupacc != nil {
return nil, nil, nil, errors.New("email_taken")
}
dupworkspace, err := s.r.GetWorkspaceByName(workspaceName)
if dupworkspace != nil {
return nil, nil, nil, errors.New("workspace_taken")
}
// Create all needed objects
t := time.Now().UTC()
workspace := &Workspace{
ID: uuid.Must(uuid.NewV4(), nil).String(),
Name: workspaceName,
CreatedAt: t,
AllowExternalSharing: true,
EUVAT: "",
ExternalBillingEmail: email,
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
acc := &Account{
ID: uuid.Must(uuid.NewV4(), nil).String(),
Name: name,
Email: email,
Password: string(hash),
CreatedAt: t,
EmailConfirmationSentTo: email,
EmailConfirmed: false,
EmailConfirmationKey: uuid.Must(uuid.NewV4(), nil).String(),
EmailConfirmationPending: true,
PasswordResetKey: uuid.Must(uuid.NewV4(), nil).String(),
}
sub := &Subscription{
ID: uuid.Must(uuid.NewV4(), nil).String(),
WorkspaceID: workspace.ID,
Level: "TRIAL",
NumberOfEditors: 100,
FromDate: t,
ExpirationDate: t.AddDate(0, 0, 15),
CreatedByName: acc.Name,
CreatedAt: t,
LastModified: t,
LastModifiedByName: acc.Name,
Status: "trialing",
}
if s.config.Mode != "hosted" {
sub.Level = "BASIC"
sub.NumberOfEditors = 1000
sub.ExpirationDate = t.AddDate(1000, 0, 0)
sub.Status = "active"
}
member := &Member{
ID: uuid.Must(uuid.NewV4(), nil).String(),
WorkspaceID: workspace.ID,
AccountID: acc.ID,
Level: "OWNER",
CreatedAt: t,
}
s.r.StoreWorkspace(workspace)
s.r.StoreAccount(acc)
s.r.StoreSubscription(sub)
s.r.StoreMember(member)
s.SetWorkspaceObject(workspace)
s.SetAccountObject(acc)
s.SetSubscriptionObject(sub)
s.SetMemberObject(member)
body, err := WelcomeBody(welcome{s.config.AppSiteURL, acc.EmailConfirmationSentTo, workspace.Name, acc.EmailConfirmationKey})
if err != nil {
log.Println(err)
return nil, nil, nil, err
}
err = s.SendEmail(s.config.SMTPServer, s.config.SMTPPort, s.config.SMTPUser, s.config.SMTPPass, s.config.EmailFrom, acc.EmailConfirmationSentTo, "Welcome to Featmap!", body)
if err != nil {
log.Println("error sending mail")
}
return workspace, acc, member, nil
}
func (s *service) DeleteAccount() error {
members := s.GetMembersByAccount()
for _, m := range members {
if m.Level == "OWNER" {
return errors.New("Cannot delete account if owner of workspace. ")
}
}
s.r.DeleteAccount(s.Acc.ID)
return nil
}
func (s *service) Login(email string, password string) (*Account, error) {
acc, err := s.r.GetAccountByEmail(strings.ToLower(email))
if acc == nil {
return nil, errors.Wrap(err, "email not found")
}
if err := bcrypt.CompareHashAndPassword([]byte(acc.Password), []byte(password)); err != nil {
return nil, errors.Wrap(err, "password not correct")
}
return acc, nil
}
func (s *service) Token(accountID string) string {
_, tokenString, _ := s.auth.Encode(jwt.MapClaims{"id": accountID})
return tokenString
}
func (s *service) GetAccount(id string) (*Account, error) {
acc, err := s.r.GetAccount(id)
if acc == nil {
return nil, errors.Wrap(err, "account not found")
}
return acc, nil
}
func (s *service) GetAccountsByWorkspace() []*Account {
accounts, err := s.r.FindAccountsByWorkspace(s.Member.WorkspaceID)
if err != nil {
log.Println(err)
return nil
}
return accounts
}
func (s *service) GetSubscriptionsByAccount() []*Subscription {
subs, err := s.r.FindSubscriptionsByAccount(s.Acc.ID)
if err != nil {
return nil
}
return subs
}
func (s *service) GetSubscriptionByWorkspace(id string) *Subscription {
subs, err := s.r.FindSubscriptionsByWorkspace(id)
if err != nil {
return nil
}
// Take the latest one, they come sorted from the db!
return subs[0]
}
func workspaceNameIsValid(name string) bool {
return !(len(name) < 2 || len(name) > 200 || !govalidator.IsAlphanumeric(name) || name == "account" || name == "link")
}
func (s *service) CreateWorkspace(name string) (*Workspace, *Subscription, *Member, error) {
name = govalidator.Trim(name, "")
if !workspaceNameIsValid(name) {
return nil, nil, nil, errors.New("workspace_invalid")
}
dupworkspace, _ := s.r.GetWorkspaceByName(name)
if dupworkspace != nil {
return nil, nil, nil, errors.New("workspace_taken")
}
t := time.Now().UTC()
workspace := &Workspace{
ID: uuid.Must(uuid.NewV4(), nil).String(),
Name: name,
CreatedAt: t,
AllowExternalSharing: true,
EUVAT: "",
ExternalBillingEmail: s.Acc.Email,
}
subscription := &Subscription{
ID: uuid.Must(uuid.NewV4(), nil).String(),
WorkspaceID: workspace.ID,
Level: "TRIAL",
NumberOfEditors: 100,
FromDate: t,
ExpirationDate: t.AddDate(0, 0, 15),
CreatedByName: s.Acc.Name,
CreatedAt: t,
LastModified: t,
LastModifiedByName: s.Acc.Name,
Status: "trialing",
}
member := &Member{
ID: uuid.Must(uuid.NewV4(), nil).String(),
WorkspaceID: workspace.ID,
AccountID: s.Acc.ID,
Level: "OWNER",
CreatedAt: t,
}
s.r.StoreWorkspace(workspace)
s.r.StoreSubscription(subscription)
s.r.StoreMember(member)
return workspace, subscription, member, nil
}
func (s *service) GetWorkspace(id string) (*Workspace, error) {
workspace, err := s.r.GetWorkspace(id)
if err != nil {
return nil, errors.Wrap(err, "workspace not found")
}
return workspace, nil
}
func (s *service) GetWorkspaceByContext() *Workspace {
workspace, err := s.GetWorkspace(s.Member.WorkspaceID)
if err != nil {
log.Println(err)
}
return workspace
}
func (s *service) GetWorkspaces() []*Workspace {
workspace, err := s.r.GetWorkspacesByAccount(s.Acc.ID)
if err != nil {
log.Println(err)
return nil
}
return workspace
}
func (s *service) UpdateMemberLevel(memberID string, level string) (*Member, error) {
if !levelIsValid(level) {
return nil, errors.New("level invalid")
}
member, err := s.r.GetMember(s.Member.WorkspaceID, memberID)
if err != nil {
return nil, err
}
if member.ID == s.Member.ID {
return nil, errors.New("not allowed to change own role")
}
if member.Level == "OWNER" && s.Member.Level == "ADMIN" {
return nil, errors.New("not allowed to change role of owner")
}
if isEditor(level) {
members := s.GetMembers()
sub := s.GetSubscriptionByWorkspace(s.Member.WorkspaceID)
n := 0
for _, m := range members {
if isEditor(m.Level) {
n++
}
}
if (!isEditor(member.Level)) && (n+1 > sub.NumberOfEditors) {
return nil, errors.New("subscription exceeded - please contact the owner of the workspace")
}
}
member.Level = level
s.r.StoreMember(member)
return member, nil
}
func (s *service) numberOfEditors() int {
members := s.GetMembers()
n := 0
for _, m := range members {
if isEditor(m.Level) {
n++
}
}
return n
}
func (s *service) DeleteMember(id string) error {
member, err := s.r.GetMember(s.Member.WorkspaceID, id)
if err != nil {
return err
}
if member.ID == s.Member.ID && s.Member.Level == "OWNER" {
return errors.New("owners not allowed to remove their own membership")
}
if member.Level == "OWNER" && s.Member.Level == "ADMIN" {
return errors.New("admins not allowed to remove membership of owner ")
}
s.r.DeleteMember(s.Member.WorkspaceID, id)
return nil
}
func isEditor(level string) bool {
return level == "EDITOR" || level == "ADMIN" || level == "OWNER"
}
func (s *service) CreateMember(workspaceID string, accountID string, level string) (*Member, error) {
sub := s.GetSubscriptionByWorkspace(workspaceID)
if sub.Level == "NONE" {
return nil, errors.New("cannot create member on workspace without plan")
}
if isEditor(level) {
members := s.GetMembersByWorkspace(workspaceID)
n := 0
for _, m := range members {
if isEditor(m.Level) {
n++
}
}
if n >= sub.NumberOfEditors {
return nil, errors.New("subscription exceeded - please contact the owner of the workspace")
}
}
// Store member
member := &Member{
ID: uuid.Must(uuid.NewV4(), nil).String(),
WorkspaceID: workspaceID,
AccountID: accountID,
Level: level,
CreatedAt: time.Now().UTC(),
}
s.r.StoreMember(member)
return member, nil
}
func (s *service) GetMember(accountID string, workspaceID string) (*Member, error) {
member, err := s.r.GetMemberByAccountAndWorkspace(accountID, workspaceID)
if member == nil {
return nil, errors.Wrap(err, "member not found")
}
return member, nil
}
func (s *service) GetMembersByAccount() []*Member {
members, err := s.r.GetMembersByAccount(s.Acc.ID)
if err != nil {
log.Println(err)
return nil
}
return members
}
func (s *service) GetMembers() []*Member {
members, err := s.r.FindMembersByWorkspace(s.Member.WorkspaceID)
if err != nil {
log.Println(err)
return nil
}
return members
}
func (s *service) GetMembersByWorkspace(id string) []*Member {
members, err := s.r.FindMembersByWorkspace(id)
if err != nil {
return nil
}
return members
}
// SETTINGS
func (s *service) ChangeAllowExternalSharing(value bool) error {
w := s.GetWorkspaceByContext()
w.AllowExternalSharing = value
s.r.StoreWorkspace(w)
return nil
}
func (s *service) ChangeGeneralInfo(EUVAT string, externalBillingInfo string) error {
w := s.GetWorkspaceByContext()
if !govalidator.IsEmail(externalBillingInfo) {
return errors.New("invalid email")
}
w.EUVAT = EUVAT
w.ExternalBillingEmail = externalBillingInfo
if len(w.ExternalCustomerID) > 0 {
params := &stripe.CustomerParams{}
params.Email = stripe.String(externalBillingInfo)
_, err := customer.Update(w.ExternalCustomerID, params)
if err != nil {
return err
}
}
s.r.StoreWorkspace(w)
return nil
}
// INVITES
func (s *service) CreateInvite(email string, level string) (*Invite, error) {
email = strings.ToLower(govalidator.Trim(email, ""))
if !govalidator.IsEmail(email) {
return nil, errors.New("email invalid")
}
if !levelIsValid(level) {
return nil, errors.New("level invalid")
}
if s.Member.Level == "ADMIN" && level == "OWNER" {
return nil, errors.New("admins are not allowed to appoint new owners")
}
member, _ := s.r.GetMemberByEmail(s.Member.WorkspaceID, email)
if member != nil {
return nil, errors.New("already member of the workspace")
}
m, _ := s.r.GetInviteByEmail(s.Member.WorkspaceID, email)
if m != nil {
return nil, errors.New("email already has a pending invite")
}
ws, err := s.r.GetWorkspace(s.Member.WorkspaceID)
if err != nil {
return nil, err
}
x := &Invite{
WorkspaceID: s.Member.WorkspaceID,
ID: uuid.Must(uuid.NewV4(), nil).String(),
Email: email,
Level: level,
Code: uuid.Must(uuid.NewV4(), nil).String(),
CreatedBy: s.Member.ID,
CreatedByName: s.Acc.Name,
CreatedAt: time.Now().UTC(),
CreatedByEmail: s.Acc.Email,
WorkspaceName: ws.Name,
}
s.r.StoreInvite(x)
if err := s.SendInvitationMail(x.ID); err != nil {
return nil, err
}
return x, nil
}
func (s *service) SendInvitationMail(invitationID string) error {
ws, err := s.r.GetWorkspace(s.Member.WorkspaceID)
if err != nil {
return err
}
invite, err := s.r.GetInvite(s.Member.WorkspaceID, invitationID)
if err != nil {
return err
}
i := InviteStruct{
AppSiteURL: s.config.AppSiteURL,
WorkspaceName: ws.Name,
Email: invite.Email,
Code: invite.Code,
InvitedBy: invite.CreatedByName,
InvitedByEmail: invite.CreatedByEmail,
}
body, err := inviteBody(i)
if err != nil {
return err
}
err = s.SendEmail(s.config.SMTPServer, s.config.SMTPPort, s.config.SMTPUser, s.config.SMTPPass, s.config.EmailFrom, invite.Email, "Featmap: invitation to join a workspace", body)
if err != nil {
log.Println("error sending mail")
}
return nil
}
func (s *service) DeleteInvite(id string) error {
m, err := s.r.GetInvite(s.Member.WorkspaceID, id)
if err != nil {
return err
}
if s.Member.Level == "ADMIN" && m.Level == "OWNER" {
return errors.New("admins are not allowed to cancel invite to owner")
}
s.r.DeleteInvite(s.Member.WorkspaceID, id)
return nil
}
func (s *service) Leave() error {
if s.Member.Level == "OWNER" {
return errors.New("owners cannot not themselves leave a workspace")
}
s.r.DeleteMember(s.Member.WorkspaceID, s.Member.ID)
return nil
}
func (s *service) DeleteWorkspace() error {
s.r.DeleteWorkspace(s.Member.WorkspaceID)
return nil
}
func (s *service) GetInvitesByWorkspace() []*Invite {
invites, err := s.r.FindInvitesByWorkspace(s.Member.WorkspaceID)
if err != nil {
log.Println(err)
return nil
}
return invites
}
func (s *service) AcceptInvite(code string) error {
invite, err := s.r.GetInviteByCode(code)
if err != nil {
return errors.New("invite not found")
}
acc, err := s.r.GetAccountByEmail(invite.Email)
if err != nil {
return errors.New("Please create an account first (using " + invite.Email + ") and then accept again.")
}
if _, err := s.CreateMember(invite.WorkspaceID, acc.ID, invite.Level); err != nil {
return err
}
s.r.DeleteInvite(invite.WorkspaceID, invite.ID)
return nil
}
func (s *service) GetInvite(code string) (*Invite, error) {
invite, err := s.r.GetInviteByCode(code)
if err != nil {
return nil, err
}
return invite, nil
}
// const datelayout = "2006-01-02"
// Projects
func (s *service) GetProject(id string) *Project {
pp, err := s.r.GetProject(s.Member.WorkspaceID, id)
if err != nil {
log.Println(err)
}
return pp
}
func (s *service) GetProjectByExternalLink(link string) (*Project, error) {
return s.r.GetProjectByExternalLink(link)
}
func (s *service) GetProjectExtendedByExternalLink(link string) (*projectResponse, error) {
project, err := s.r.GetProjectByExternalLink(link)
if err != nil {
return nil, err
}
milestones, err := s.r.FindMilestonesByProject(project.WorkspaceID, project.ID)
if err != nil {
return nil, err
}
workflows, err := s.r.FindWorkflowsByProject(project.WorkspaceID, project.ID)
if err != nil {
return nil, err
}
subworkflows, err := s.r.FindSubWorkflowsByProject(project.WorkspaceID, project.ID)
if err != nil {
return nil, err
}
features, err := s.r.FindFeaturesByProject(project.WorkspaceID, project.ID)
if err != nil {
return nil, err
}
featureComments, err := s.r.FindFeatureCommentsByProject(project.WorkspaceID, project.ID)
if err != nil {
return nil, err
}
personas, err := s.r.FindPersonasByProject(project.WorkspaceID, project.ID)
if err != nil {
return nil, err
}
workflowPersonas, err := s.r.FindWorkflowPersonasByProject(project.WorkspaceID, project.ID)
if err != nil {
return nil, err
}
resp := &projectResponse{
Project: project,
Milestones: milestones,
Workflows: workflows,
SubWorkflows: subworkflows,
Features: features,
FeatureComments: featureComments,
Personas: personas,
WorkflowPersonas: workflowPersonas,
}
return resp, nil
}
func (s *service) CreateProjectWithID(id string, title string) (*Project, error) {
title, err := validateTitle(title)
if err != nil {
return nil, err
}
pp, _ := s.r.GetProject(s.Member.WorkspaceID, id)
if pp != nil {
return nil, errors.New("already exist")
}
p := &Project{
WorkspaceID: s.Member.WorkspaceID,
ID: id,
Title: title,
CreatedAt: time.Now().UTC(),
CreatedByName: s.Acc.Name,
ExternalLink: uuid.Must(uuid.NewV4(), nil).String(),
}
p.LastModified = time.Now().UTC()
p.LastModifiedByName = s.Acc.Name
s.r.StoreProject(p)
return p, nil
}
func (s *service) LoadSampleCards(pid string) error {
// wsid := s.Member.WorkspaceID
// accid := s.Acc.ID
// t := time.UTC()
// milestone1 := &Milestone{WorkspaceID: wsid, ProjectID: pid, ID: newUIID(), Title: "Proof-of-concept", Status: "OPEN", }
return nil
}
func (s *service) RenameProject(id string, title string) (*Project, error) {
title, err := validateTitle(title)
if err != nil {
return nil, err
}
p, err := s.r.GetProject(s.Member.WorkspaceID, id)
if err != nil {
return nil, err
}
p.Title = title
p.LastModified = time.Now().UTC()
p.LastModifiedByName = s.Acc.Name
s.r.StoreProject(p)
return p, nil
}
func (s *service) UpdateProjectDescription(id string, d string) (*Project, error) {
x, err := s.r.GetProject(s.Member.WorkspaceID, id)
if err != nil {
return nil, err
}
x.Description = d
x.LastModified = time.Now().UTC()
x.LastModifiedByName = s.Acc.Name
s.r.StoreProject(x)
return x, nil
}
func (s *service) DeleteProject(id string) error {
s.r.DeleteProject(s.Member.WorkspaceID, id)
return nil
}
func (s *service) GetProjects() []*Project {
pp, err := s.r.FindProjectsByWorkspace(s.Member.WorkspaceID)
if err != nil {
log.Println(err)
}
return pp
}
// Milestones
func (s *service) CreateMilestoneWithID(id string, projectID string, title string) (*Milestone, error) {
title, err := validateTitle(title)
if err != nil {
return nil, err
}
a, _ := s.r.GetMilestone(s.Member.WorkspaceID, id)
if a != nil {
return nil, errors.New("already exists")
}
mm, _ := s.r.FindMilestonesByProject(s.Member.WorkspaceID, projectID)
p := &Milestone{
WorkspaceID: s.Member.WorkspaceID,
ProjectID: projectID,
ID: id,
Title: title,
Status: "OPEN",
Rank: "",
CreatedAt: time.Now().UTC(),
CreatedByName: s.Acc.Name,
Color: "WHITE",
}
n := len(mm)
if n == 0 {
rank, _ := lexorank.Rank("", "")
p.Rank = rank