def mail_member_send(sender, **kwargs):
    team_member_change_db = TeamMemberChangedDB()
    awsses = AWSSES(aws_access_key_id=setting.AWS_ID,
                    aws_secret_access_key=setting.AWS_KEY,
                    source=setting.AWS_SES_FROM)

    resp = awsses.send_raw_email(data_str=kwargs['raw_mail'])
    logger.info(resp)
    if resp['ResponseMetadata']['HTTPStatusCode'] != 200:
        raise Exception('HTTPStatusCode not `200`, do retry')

    team_member_change_db.find_one_and_update({'_id': ObjectId(kwargs['rid'])},
                                              {'$set': {
                                                  'done.mail': True
                                              }})
def service_sync_gsuite_memberchange(sender, **kwargs):
    team_member_change_db = TeamMemberChangedDB()
    sync_gsuite = None
    for raw in team_member_change_db.find(
        {
            'done.gsuite_team': {
                '$exists': False
            },
            'case': {
                '$in': ('add', 'del')
            }
        },
            sort=(('create_at', 1), )):
        team = Team.get(raw['pid'], raw['tid'])

        if 'mailling' not in team or not team['mailling']:
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {
                    'done.gsuite_team': True
                }})
            continue

        if sync_gsuite is None:
            sync_gsuite = SyncGSuite(credentialfile=setting.GSUITE_JSON,
                                     with_subject=setting.GSUITE_ADMIN)

        user = User(uid=raw['uid']).get()
        if raw['case'] == 'add':
            sync_gsuite.add_users_into_group(group=team['mailling'],
                                             users=(user['mail'], ))
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {
                    'done.gsuite_team': True
                }})

        elif raw['case'] == 'del':
            sync_gsuite.del_users_from_group(group=team['mailling'],
                                             users=(user['mail'], ))
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {
                    'done.gsuite_team': True
                }})

    for raw in team_member_change_db.find(
        {
            'done.gsuite_staff': {
                '$exists': False
            },
            'case': {
                '$in': ('add', 'del')
            }
        },
            sort=(('create_at', 1), )):
        project = Project.get(raw['pid'])

        if 'mailling_staff' not in project or not project['mailling_staff']:
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {
                    'done.gsuite_staff': True
                }})
            continue

        if sync_gsuite is None:
            sync_gsuite = SyncGSuite(credentialfile=setting.GSUITE_JSON,
                                     with_subject=setting.GSUITE_ADMIN)

        user = User(uid=raw['uid']).get()
        if raw['case'] == 'add':
            sync_gsuite.add_users_into_group(group=project['mailling_staff'],
                                             users=(user['mail'], ))
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {
                    'done.gsuite_staff': True
                }})

        elif raw['case'] == 'del':
            sync_gsuite.del_users_from_group(group=project['mailling_staff'],
                                             users=(user['mail'], ))
            team_member_change_db.find_one_and_update(
                {'_id': raw['_id']}, {'$set': {
                    'done.gsuite_staff': True
                }})