def update_displayed_orders(self, channel: Channel): orders = list(self.orders.find_all(today())) for cmd_message in self.messages.find_all('orders', date=today()): new_ts = self.api.update_message( channel.id, cmd_message.timestamp, Message(blocks=list(self.ui.all_orders(orders)))).ts self.messages.update_timestamp(cmd_message.id, new_ts)
def display_orders(self, channel: Channel) -> SlackMessage: orders = list(self.orders.find_all(today())) reply = self.api.post_message( channel.id, Message(blocks=list(self.ui.all_orders(orders)))) message = SlackMessage.create_today('orders', reply.ts, reply.channel) self.messages.create(message) return message
def create_new_order(self, user: User): order = self.orders.find(today(), user) if order is None: order = self.orders.create(user) return Message(self.display_order(order), response_type=MessageType.ephemeral, attachments=self.ui.create_order_attachments(self.menu, order))
def add_dessert(self, action: BlockAction, user: User): order = self.orders.find(today(), user) dessert = self.find_dessert(action.selected_option.value) if dessert: order = replace(order, desserts=order.desserts + [dessert]) self.orders.update(order) return Message.replace(self.ui.my_order(self.menu, order))
def add_box(self, action: BlockAction, user: User): order = self.orders.find(today(), user) box = self.find_box(action.selected_option.value) if box: order = replace(order, boxes=order.boxes + [box]) self.orders.update(order) return Message.replace(self.ui.my_order(self.menu, order))
def pref_user(self) -> Optional[UserPoints]: today_users = [ order.user.id for order in self.orders.find_all(today()) ] candidates = [ up for up in self.find_all() if up.user.id in today_users ] if candidates: return candidates[0]
def complete_order(self, channel: Channel, user: User, new_order: bool): cmds = list(self.messages.find_all('orders', date=today())) if len(cmds) == 0: cmds = [self.display_orders(channel)] link = self.api.get_permalink(cmds[-1].channel_id, cmds[-1].timestamp) if new_order: message = f'{user.display_name} placed an order\n<{link}|View all orders>' else: message = f'{user.display_name} updated an order\n<{link}|View all orders>' self.api.post_message(channel.id, Message(message)) self.update_displayed_orders(channel)
def order_confirm(self, channel: Channel, user: User): order = self.orders.find(today(), user) new_order = not order.complete points = self.update_points(order) order = replace(order, complete=True, in_edition=False, points_given=points) self.orders.update(order) self.complete_order(channel, user, new_order) return Message.replace(self.ui.my_order(self.menu, order))
def complete_order(self, channel: Channel, user: User, new_order: bool): cmds = list(self.messages.find_all('orders', date=today())) if len(cmds) == 0: cmds = [self.display_orders(channel)] link = self.api.get_permalink(channel.id, cmds[-1].timestamp) if new_order: message = '{} placed an order\n<{}|View all orders>' else: message = '{} updated an order\n<{}|View all orders>' msg = Message(attachments=[ Attachment('', message.format(user.display_name, link), natcolor) ]) self.api.post_message(channel.id, msg) self.update_displayed_orders(channel)
def create(self, user: User) -> Order: order = Order(str(uuid4()), user, today()) self.collection.insert_one(serialize(order)) return order
def create_new_order(self, user: User): order = self.orders.find(today(), user) if order is None: order = self.orders.create(user) return Message.ephemeral(self.ui.my_order(self.menu, order))
def order_edit(self, user: User): order = self.orders.find(today(), user) order = replace(order, in_edition=True) self.orders.update(order) return Message.replace(self.ui.my_order(self.menu, order))
def order_cancel(self, user: User): order = self.orders.find(today(), user) self.orders.delete(order.id) self.points.update(order.user, -1 * order.points_given) return Message.delete()
def display_orders(self, channel: Channel) -> SlackMessage: orders = list(self.orders.find_all(today())) timestamp = self.api.post_message(channel.id, Message(self.ui.orders_text(orders))) message = SlackMessage.create_today('orders', timestamp) self.messages.create(message) return message
def clear_dessert(self, user: User): order = self.orders.find(today(), user) order = replace(order, desserts=[]) self.orders.update(order) return Message.replace(self.ui.my_order(self.menu, order))
def create_today(type: str, timestamp: str) -> 'SlackMessage': return SlackMessage(str(uuid4()), type, today(), timestamp)
def create_today(type: str, timestamp: str, channel_id: Optional[str] = None) -> 'SlackMessage': return SlackMessage(str(uuid4()), type, today(), timestamp, channel_id)
def update_displayed_orders(self, channel: Channel): orders = list(self.orders.find_all(today())) for cmd_message in self.messages.find_all('orders', date=today()): new_ts = self.api.update_message(channel.id, cmd_message.timestamp, Message(text=self.ui.orders_text(orders))) self.messages.update_timestamp(cmd_message._id, new_ts)