async def get_accidents_by_user_id_master( self, *, user_search: ProfileSearch, populate: bool = True) -> List[AccidentPublic]: accident_records = await self.db.fetch_all( query=GET_ACCIDENTS_BY_USER_STMT_ID_QUERY, values={"user_id": user_search.user_id}) accidents_declares = await self.db.fetch_all( query=GET_ACCIDENTS_BY_TEMPORARY_DRIVER_EMAIL_QUERY, values={"driver_email": user_search.email}) accidents_list = [] accident_id_list = [] if not accident_records and not accidents_declares: return None else: for accident_record in accident_records: accident_id_list.append(accident_record["id"]) accident = AccidentInDB(**accident_record) if populate: accident_populated = await self.populate_accident( accident=accident) accidents_list.append(accident_populated) for accident_declare in accidents_declares: if accident_declare['id'] in accident_id_list: pass else: accident = AccidentInDB(**accident_declare) if populate: accident_populated = await self.populate_accident( accident=accident) accidents_list.append(accident_populated) return accidents_list
async def create_accident_for_vehicle(self, *, new_accident: AccidentCreate, vehicle_id: int, id: int) -> AccidentInDB: query_values = new_accident.dict(exclude_unset=True) new_accident = await self.db.fetch_one( query=CREATE_ACCIDENT_FOR_VEHICLE_QUERY, values=query_values) await self.accident_statement_repo.create_accident_statement( vehicle_id=vehicle_id, user_id=id, accident_id=new_accident["id"]) return await self.populate_accident(accident=AccidentInDB( **new_accident))
async def get_all_accidents(self, *, populate: bool = True) -> List[AccidentPublic]: accident_records = await self.db.fetch_all( query=GET_ALL_ACCIDENTS_QUERY) accidents_list = [] for accident_record in accident_records: accident = AccidentInDB(**accident_record) if populate: accident_populated = await self.populate_accident( accident=accident) accidents_list.append(accident_populated) return accidents_list
async def get_accident_by_id(self, *, id: int, populate: bool = True) -> AccidentInDB: accident_record = await self.db.fetch_one( query=GET_ACCIDENT_BY_ID_QUERY, values={"id": id}) if not accident_record: return None else: accident = AccidentInDB(**accident_record) if populate: return await self.populate_accident(accident=accident) return accident
async def update_closed_case(self, *, id: int, populate: bool = True) -> AccidentPublic: accident = await self.get_accident_by_id(id=id) if not accident: return None else: updated_accident = await self.db.fetch_one( query=UPDATE_CLOSED_CASE_QUERY, values={"id": id}) accident_closed = AccidentInDB(**updated_accident) if populate: accident_populated = await self.populate_accident( accident=accident_closed) return accident_populated
async def get_all_accidents_by_insurance_company( self, *, insurance_company_email: EmailStr, populate: bool = True) -> List[AccidentPublic]: accident_records = await self.db.fetch_all( query=GET_ALL_ACCIDENTS_BY_INSURANCE_COMPANY, values={"insurance_company_email": insurance_company_email}) accidents_list = [] for accident_record in accident_records: accident = AccidentInDB(**accident_record) if populate: accident_populated = await self.populate_accident( accident=accident) accidents_list.append(accident_populated) return accidents_list
async def get_accident_by_temporary_driver_by_email( self, *, id: int, email: EmailStr, populate: bool = True) -> AccidentPublic: accident_record = await self.db.fetch_one( query=GET_ACCIDENT_BY_TEMPORARY_DRIVER_EMAIL_ACCIDENT_ID_QUERY, values={ "id": id, "email": email }) if not accident_record: return None else: accident = AccidentInDB(**accident_record) if populate: return await self.populate_accident(accident=accident) return accident
async def get_accident_from_user_with_statement_id(self, *, id: int, user_id: int, populate: bool = True ) -> AccidentPublic: accident_record = await self.db.fetch_one( query=GET_ACCIDENT_BY_USER_ID_WITH_STATEMENT_QUERY, values={ "id": id, "user_id": user_id }) if not accident_record: return None else: accident = AccidentInDB(**accident_record) if populate: return await self.populate_accident(accident=accident) return accident