def update_device_token(cmd: commands.UpdateDeviceToken, uow: unit_of_work.AbstractUnitOfWork): with uow: device = uow.devices.find_by_device_id(cmd.device_id) device.device_token = cmd.device_token uow.devices.add(device) uow.commit()
def reset_password(cmd: commands.ResetPassword, uow: unit_of_work.AbstractUnitOfWork): app_id = cmd.app_id with uow: token = cmd.token email = cmd.email if token and email: new_password = cmd.new_password dec_token = decrypt(token).split(':') expire_date = int(dec_token[1]) time_now = int(time.time()) # check validate Token if expire_date < time_now: raise InvalidToken("Token invalid") # Get User Data token_conditions = {"token": token} token = uow.used_tokens.get_one(token_conditions) if token.used: raise InvalidToken("Token invalid") else: user = uow.users.get_by_email(email, app_id) user.password = encode_password(new_password) user.change_pass_time = time_now uow.users.add(user) user.reset_password() # save Token To prevent using it again .... token.used = 1 uow.used_tokens.add(token) uow.commit()
def change_batch_quantity( event: events.BatchQuantityChanged, uow: unit_of_work.AbstractUnitOfWork ): with uow: product = uow.products.get_by_batchref(batchref=event.ref) product.change_batch_quantity(ref=event.ref, qty=event.qty) uow.commit()
def attach_device(cmd: commands.AttachDeviceToUser, uow: unit_of_work.AbstractUnitOfWork): with uow: device = UserDevices(user_id=cmd.device_data.user_id, device_id=cmd.device_data.device_id, device_token=cmd.device_data.device_token, device_type=cmd.device_data.device_type) uow.devices.create_or_update(device) uow.commit()
def add_batch( event: events.BatchCreated, uow: unit_of_work.AbstractUnitOfWork ): with uow: product = uow.products.get(sku=event.sku) if product is None: product = model.Product(event.sku, batches=[]) uow.products.add(product) uow.batches.append(model.Batch(event.ref, event.sku, event.qty, event.eta)) uow.commit()
def allocate( event: events.AllocationRequired, uow: unit_of_work.AbstractUnitOfWork ) -> str: line = OrderLine(event.orderid, event.sku, event.qty) with uow: product = uow.products.get(sku=event.sku) if product is None: raise InvalidSku(f'Invalid SKU {line.sku}') batchref = product.allocate(line) uow.commit() return batchref
def add_meeting(data: Dict[str, Any], uow: AbstractUnitOfWork) -> Dict[str, Any]: meeting: MeetingEntity = MeetingService.schema().load(data) with uow: try: evenement: EvenementEntity = uow.evenement.get_by_uuid( meeting.evenement_id) meeting.evenement_id = evenement.uuid meeting.creator_id = data["creator_id"] uow.meeting.add(meeting) user: UserEntity = uow.user.get_by_uuid( uuid=data["creator_id"]) meeting.set_creator(creator=user) uow.commit() return MeetingService.schema().dump( uow.meeting.get_by_uuid(meeting.uuid)) finally: event_bus.publish(MeetingCreatedEvent(data=meeting), uow=uow)
def create_user(cmd: commands.CreateUser, uow: unit_of_work.AbstractUnitOfWork): with uow: user_data = cmd.user_data.__dict__ user_data["app_id"] = cmd.app_id data = prepare_request_data(user_data) login_type = data["external"] if login_type != "Boltplay": # add user as external external_gateway_conditions = {"gateway_name": login_type} external_gateways = uow.external_gateways.all( external_gateway_conditions) data["external"] = external_gateways[0].get("id", None) else: data["external"] = None conditions = { "app_id": data["app_id"], "email": user_data["email"], "external": user_data["external"] } users_list = uow.users.all(conditions) # Check only in case users are internal if len(users_list) > 0 and user_data["external"] is None: raise InvalidEmail("Email already exists") if users_list: user = users_list[0] else: user = User(email=data["email"], password=data["password"], name=data["name"], app_id=data["app_id"], phone=data["phone"], user_id=data["user_id"], firebase_id=data["firebase_id"], external=data["external"], devices=[]) uow.users.add(user) uow.commit() user.create(login_type) cmd.user_id = user.id
def send_reset_email(cmd: commands.SendResetEmail, uow: unit_of_work.AbstractUnitOfWork): app_id = cmd.app_id with uow: expire = int(time.time()) + RESET_PASS_TOKEN # Get user data user = uow.users.get_by_email(cmd.email, app_id) if not user: raise InvalidEmail("Email not found") token = "RESET_PASS,user_email %s:%d" % (cmd.email, expire) token_encoded = encrypt(token) if isinstance(user, list): for x in user: user_data = { "app_id": get_owner_id(app_id), "name": x.name, "email": cmd.email, "kwargs": { "token": token_encoded } } x.send_reset_password() else: user_data = { "app_id": get_owner_id(app_id), "name": user.name, "email": cmd.email, "kwargs": { "token": token_encoded } } user.send_reset_password() cmd.token_encoded = token_encoded token = UsedToken(token=token_encoded) uow.used_tokens.add(token) uow.commit()