Batch import wasn't updated

This commit is contained in:
Carlos-Mesquita
2024-11-06 11:01:39 +00:00
parent e51cd891d2
commit a2e96f8e54
18 changed files with 124 additions and 78 deletions

View File

@@ -2,20 +2,17 @@ import os
import subprocess
import time
import uuid
import pandas as pd
import shortuuid
from datetime import datetime
from logging import getLogger
import pandas as pd
from typing import Dict
import shortuuid
from pymongo.database import Database
from app.dtos.user_batch import BatchUsersDTO, UserDTO
from app.helpers import FileHelper
from app.repositories.abc import IDocumentStore
from app.services.abc import IUserService
@@ -34,14 +31,14 @@ class UserService(IUserService):
"speaking": 0,
}
def __init__(self, mongo: Database):
self._db: Database = mongo
def __init__(self, document_store: IDocumentStore):
self._db = document_store
self._logger = getLogger(__name__)
def fetch_tips(self, batch: BatchUsersDTO):
def batch_users(self, batch_dto: BatchUsersDTO):
file_name = f'{uuid.uuid4()}.csv'
path = f'./tmp/{file_name}'
self._generate_firebase_auth_csv(batch, path)
self._generate_firebase_auth_csv(batch_dto, path)
result = self._upload_users('./tmp', file_name)
if result.returncode != 0:
@@ -49,20 +46,11 @@ class UserService(IUserService):
self._logger.error(error_msg)
return error_msg
self._init_users(batch)
self._init_users(batch_dto)
FileHelper.remove_file(path)
return {"ok": True}
@staticmethod
def _map_to_batch(request_data: Dict) -> BatchUsersDTO:
users_list = [{**user} for user in request_data["users"]]
for user in users_list:
user["studentID"] = str(user["studentID"])
users: list[UserDTO] = [UserDTO(**user) for user in users_list]
return BatchUsersDTO(makerID=request_data["makerID"], users=users)
@staticmethod
def _generate_firebase_auth_csv(batch_dto: BatchUsersDTO, path: str):
# https://firebase.google.com/docs/cli/auth#file_format
@@ -127,22 +115,21 @@ class UserService(IUserService):
result = subprocess.run(command, shell=True, cwd=directory, capture_output=True, text=True)
return result
def _init_users(self, batch_users: BatchUsersDTO):
async def _init_users(self, batch_users: BatchUsersDTO):
maker_id = batch_users.makerID
for user in batch_users.users:
self._insert_new_user(user)
code = self._create_code(user, maker_id)
await self._insert_new_user(user)
await self._create_code(user, maker_id)
if user.groupName and len(user.groupName.strip()) > 0:
self._assign_user_to_group_by_name(user, maker_id)
await self._assign_user_to_group_by_name(user, maker_id)
def _insert_new_user(self, user: UserDTO):
async def _insert_new_user(self, user: UserDTO):
new_user = {
**user.dict(exclude={
'passport_id', 'groupName', 'expiryDate',
'corporate', 'passwordHash', 'passwordSalt'
}),
'id': str(user.id),
'bio': "",
'focus': "academic",
'status': "active",
@@ -155,11 +142,11 @@ class UserService(IUserService):
'subscriptionExpirationDate': user.expiryDate,
'entities': user.entities
}
self._db.users.insert_one(new_user)
await self._db.save_to_db("users", new_user, str(user.id))
def _create_code(self, user: UserDTO, maker_id: str) -> str:
async def _create_code(self, user: UserDTO, maker_id: str) -> str:
code = shortuuid.ShortUUID().random(length=6)
self._db.codes.insert_one({
await self._db.save_to_db("codes", {
'id': code,
'code': code,
'creator': maker_id,
@@ -170,34 +157,32 @@ class UserService(IUserService):
'email': user.email,
'name': user.name,
'passport_id': user.passport_id
})
}, code)
return code
def _assign_user_to_group_by_name(self, user: UserDTO, maker_id: str):
async def _assign_user_to_group_by_name(self, user: UserDTO, maker_id: str):
user_id = str(user.id)
groups = list(self._db.groups.find(
{
groups = await self._db.find("groups", {
"admin": maker_id,
"name": user.groupName.strip()
}
))
})
if len(groups) == 0:
new_group = {
'id': str(uuid.uuid4()),
'admin': maker_id,
'name': user.groupName.strip(),
'participants': [user_id],
'disableEditing': False,
}
self._db.groups.insert_one(new_group)
await self._db.save_to_db("groups", new_group, str(uuid.uuid4()))
else:
group = groups[0]
participants = group["participants"]
if user_id not in participants:
participants.append(user_id)
self._db.groups.update_one(
await self._db.update(
"groups",
{"id": group["id"]},
{"$set": {"participants": participants}}
)