Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transforming a normal account into a team #1106

Open
Changaco opened this issue May 17, 2018 · 1 comment
Open

Transforming a normal account into a team #1106

Changaco opened this issue May 17, 2018 · 1 comment

Comments

@Changaco
Copy link
Member

The need for this arose in #1098.

@Changaco
Copy link
Member Author

This is what I used:

    def transform_into_team(self, first_member):
        old_kind = self.kind
        assert old_kind in ('individual', 'organization')
        assert self.balance == 0
        assert self.status == 'active'
        assert first_member.kind in ('individual', 'organization')
        assert first_member.status == 'active'
        mp_account = self.get_mangopay_account()
        with self.db.get_cursor() as cursor:
            r = cursor.one("""
                UPDATE participants
                   SET kind = 'group'
                     , mangopay_user_id = NULL
                 WHERE id = %s
                   AND kind = %s
             RETURNING kind, mangopay_user_id
            """, (self.id, self.kind))
            if not r:
                raise response.error(409, "race condition")
            self.set_attributes(**r._asdict())
            if mp_account:
                self.add_event(cursor, 'mangopay-account-change', {
                    'old_user_id': mp_account.Id,
                    'old_user_type': mp_account.PersonType,
                })
            self.add_event(cursor, 'account-kind-change', {
                'old_kind': old_kind,
                'new_kind': 'group',
            })
            self.add_member(first_member, cursor=cursor)
        # Drop sessions and passwords
        self.db.run("DELETE FROM user_secrets WHERE participant = %s", (self.id,))
        # Invalidate exchange routes
        routes = self.db.all("""
            SELECT r
              FROM exchange_routes r
             WHERE participant = %s
               AND error <> 'invalidated'
        """, (self.id,))
        for route in routes:
            route.invalidate()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant