@@ -34,6 +34,8 @@ type Service interface {
3434 List (ctx context.Context , userId string ) ([]* invitations.Invitation , error )
3535 GetByInvitedEmail (ctx context.Context , email string ) (* invitations.Invitation , error )
3636 GetByInviteId (ctx context.Context , id string ) (* invitations.Invitation , error )
37+ DeleteById (ctx context.Context , id string ) error
38+ DeleteByInvitedEmail (ctx context.Context , email string ) error
3739}
3840
3941// Backend defines the behaviour of a user backend.
@@ -153,7 +155,6 @@ func (s svc) Invite(ctx context.Context, invitation *invitations.Invitation) (*i
153155
154156// List implements the service interface
155157func (s svc ) List (ctx context.Context , userId string ) ([]* invitations.Invitation , error ) {
156- fmt .Println ("list invitations for user" , userId )
157158 // get logged in user
158159 u , ok := revactx .ContextGetUser (ctx )
159160 if ! ok {
@@ -190,7 +191,51 @@ func (s svc) List(ctx context.Context, userId string) ([]*invitations.Invitation
190191 return invSlice , nil
191192}
192193
193- // GetByInviteId implements the service interface
194+ // DeleteById deletes invitation by id
195+ func (s svc ) DeleteById (ctx context.Context , id string ) error {
196+ u , ok := revactx .ContextGetUser (ctx )
197+ if ! ok {
198+ return ErrUnauthorized
199+ }
200+
201+ toDelete , err := s .Persistance .Read (id )
202+ if err != nil {
203+ return fmt .Errorf ("%w: %s" , ErrPersistence , err )
204+ }
205+
206+ if toDelete [0 ].Metadata ["inviterUserId" ] != u .GetId ().GetOpaqueId () {
207+ return ErrUnauthorized
208+ }
209+
210+ return s .Persistance .Delete (id )
211+ }
212+
213+ // DeleteByEmail deletes invitation by email of the invited person
214+ func (s svc ) DeleteByInvitedEmail (ctx context.Context , email string ) error {
215+ u , ok := revactx .ContextGetUser (ctx )
216+ if ! ok {
217+ return ErrUnauthorized
218+ }
219+ invKeyList , err := s .Persistance .List ()
220+ if err != nil {
221+ return fmt .Errorf ("%w: %s" , ErrPersistence , err )
222+ }
223+ for _ , key := range invKeyList {
224+ inv , err := s .Persistance .Read (key )
225+ if err != nil {
226+ return fmt .Errorf ("%w: %s" , ErrPersistence , err )
227+ }
228+ for _ , value := range inv {
229+ if value .Metadata ["invitedUserEmailAddress" ] == email &&
230+ u .GetId ().GetOpaqueId () == value .Metadata ["inviterUserId" ].(string ) {
231+ return s .Persistance .Delete (key )
232+ }
233+ }
234+ }
235+ return nil
236+ }
237+
238+ // GetByInviteId gets invitation by id
194239func (s svc ) GetByInviteId (ctx context.Context , inviteId string ) (* invitations.Invitation , error ) {
195240 u , ok := revactx .ContextGetUser (ctx )
196241 if ! ok {
@@ -215,7 +260,7 @@ func (s svc) GetByInviteId(ctx context.Context, inviteId string) (*invitations.I
215260 return invite , nil
216261}
217262
218- // GetByInvitedEmail implements the service interface
263+ // GetByInvitedEmail gets invitation by email
219264func (s svc ) GetByInvitedEmail (ctx context.Context , email string ) (* invitations.Invitation , error ) {
220265 u , ok := revactx .ContextGetUser (ctx )
221266 if ! ok {
0 commit comments