# Buggy code
def serialize(self) -> Dict[str, Any]:
    data = {
        "iss": self.issuer,
        "typ": self.type,
        "sub": self.subject,
        "aud": self.audience,
        "exp": self.expiration,
        "nbf": self.not_before,
        "iat": self.issued_at,
        "jti": self.jwt_id,
    }
    if self.name:
        data["name"] = self.name
    if self.preferred_username:
        data["preferred_username"] = self.preferred_username
    if self.email:
        data["email"] = self.email
    if self.provider:
        data["provider"] = self.provider
    if self.orgs:
        data["orgs"] = self.orgs
    if self.groups:
        data["groups"] = self.groups
    if self.roles:
        data["roles"] = self.roles
    if self.scopes:
        data["scope"] = " ".join(self.scopes)

    if self.email_verified is not None:
        data["email_verified"] = self.email_verified
    if self.picture is not None:
        data["picture"] = self.picture
    if current_app.config["CUSTOMER_VIEWS"]:
        data["customers"] = self.customers
    return data


# Traceback (psycopg2.errors.UniqueViolation)
Oct 15 10:45:04 influx alertad[28913]: 2020-10-15 10:45:04,542 alerta.app[28919]: [ERROR] duplicate key value violates unique constraint "users_email_key"
DETAIL:  Key (email)=(xxxxxx@xxxxx) already exists.
request_id=7f8eab74-9c03-4d36-a92f-c5c0d82b3c6f ip=80.120.197.114
Traceback (most recent call last):
File "/opt/alerta/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/alerta/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/alerta/lib/python3.7/site-packages/flask_cors/decorator.py", line 128, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "/opt/alerta/lib/python3.7/site-packages/alerta/auth/oidc.py", line 135, in openid
user.create()
File "/opt/alerta/lib/python3.7/site-packages/alerta/models/user.py", line 140, in create
return User.from_db(db.create_user(self))
File "/opt/alerta/lib/python3.7/site-packages/alerta/database/backends/postgres/base.py", line 910, in create_user
return self._insert(insert, vars(user))
File "/opt/alerta/lib/python3.7/site-packages/alerta/database/backends/postgres/base.py", line 1389, in _insert
cursor.execute(query, vars)
File "/opt/alerta/lib/python3.7/site-packages/psycopg2/extras.py", line 327, in execute
return super(NamedTupleCursor, self).execute(query, vars)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "users_email_key"
DETAIL:  Key (email)=(xxxxxx@xxxxxxxx) already exists.
Oct 15 10:45:04 influx alertad[28913]: 2020-10-15 10:45:04,541 alerta.app[28919]: [DEBUG] ****************************************

INSERT INTO users (id, name, login, password, email, status, roles, attributes,
create_time, last_login, text, update_time, email_verified)
VALUES ('Cil1aWQ9c2hlZGVuaWdnLG91PVBlb3BsZSxkYz1pbnZlbml1bSxkYz1pbxIEbGRhcA', 'xxxxxxx', 'xxxxxxxxx', '', 'xxxxxxxxxx', 'active', ARRAY['admin']
NULL, '', '2020-10-15T10:45:04.541Z', true)
RETURNING *

**************************************** request_id=7f8eab74-9c03-4d36-a92f-c5c0d82b3c6f ip=xxxxx
Oct 15 10:45:04 influx alertad[28913]: 2020-10-15 10:45:04,540 alerta.app[28919]: [DEBUG] ****************************************
SELECT * FROM users WHERE id='Cil1aWQ9c2hlZGVuaWdnLG91PVBlb3BsZSxkYz1pbnZlbml1bSxkYz1pbxIEbGRhcA'
**************************************** request_id=7f8eab74-9c03-4d36-a92f-c5c0d82b3c6f ip=xxxxx

# Fixed code
def serialize(self) -> Dict[str, Any]:
    data = {
        "iss": self.issuer,
        "typ": self.type,
        "sub": self.subject,
        "aud": self.audience,
        "exp": self.expiration,
        "nbf": self.not_before,
        "iat": self.issued_at,
        "jti": self.jwt_id,
    }
    if self.name:
        data["name"] = self.name
    if self.preferred_username:
        data["preferred_username"] = self.preferred_username
    if self.email:
        data["email"] = self.email
    if self.provider:
        data["provider"] = self.provider
    if self.orgs:
        data["orgs"] = self.orgs
    if self.groups:
        data["groups"] = self.groups
    if self.roles:
        data["roles"] = self.roles
    if self.scopes:
        data["scope"] = " ".join(self.scopes)

    if self.email_verified is not None:
        data["email_verified"] = self.email_verified
    if self.picture is not None:
        data["picture"] = self.picture
    if current_app.config["CUSTOMER_VIEWS"]:
        data["customers"] = self.customers
    if self.oid:
        data["oid"] = self.oid
    return data