Skip to content

Backend for cookie delivery applications. It can cluster orders based on location and distance from the shop. It also shows estimated time for delivery

Notifications You must be signed in to change notification settings

NightWing1998/CookieMan

Repository files navigation

COOKIE-PEOPLE

We sell cookies online to people who can buy them.

What is this repository for?

How do I get set up?

  • Summary of set up: There are 2 two ways to run this application: local and cloud.
  • Configuration: The server needs the following environment variables when in PRODUCTION mode: PORT, GRAPHQL_ROUTE, MONGODB_URI, NODE_ENV. For each and every other mode like dev or test specify these env variables with _<mode> for eg PORT_TEST in test mode
  • Dependencies: Can be found in package.json
  • Database configuration: Can be found in models
  • Deployment instructions: On local machine either run docker-compose up or install mongodb daemon, start that daemon and then npm start or npm run watch

Architecture

  1. The tech stack for the application is:

    • Backend: TypeScript, ts-node, express, apollo-server-express, graphQL, mongodb
    • Frontend: ReactJS, apollo-client
  2. The system has 3 types of user base: admin, delivery personel and customer and hence system revolves around these aspects

  3. GraphQL schema:

  	type User {
  	name: String!,
  	number: String!,
  	id: ID!,
  	currentOrders: [ID]!,
  	history: [ID]!
  }
  type token{
  	token: String!
  }
  type Order {
  	id: ID!,
  	user: User!,
  	customerAddress: String!,
  	quantity: Int!,
  	price: Int!,
  	distance: Int!,
  	barcodePath: String!,
  	status: String!,
  	deliveryPersonel: User,
  	eta: String
  }
  type Query {
  	hello : String!,
  	# Fetch All users(if non root for current logged in user only)
  	getUsers(
  		category: String, 
  		page: Int
  	): [User!]!,
  	# Fetch all orders(if non root for current logged in user only)
  	getOrders(id: ID, status: String, page: Int): [Order!]!,
  	# Long Polling query for frontend for delivery personels to accpet new orders
  	acceptOrderForDelivery(deliveryPersonelId: ID): [Order!]
  }
  type Mutation {
  	# Add user(deliveryPersonel or customer) 
  	addUser(
  		email: String!,
  		name: String!,
  		number: String!,
  		address: String,
  		password: String!,
  		category: String!
  	): token!,
  	# Place an order(customer only)
  	placeOrder(
  		customerAddress: String,
  		quantity: Int!,
  		lat: Float!,
  		long: Float!
  	): Order!,
  	# Mutation for order completion verification
  	completeOrder(
  		orderId: ID!,
  		text: String!
  	): Boolean!,
  	# Mutation to login
  	login(
  		email: String!,
  		password: String!
  	): token!
  },
  type Subscription {
  	# Subscriptions for usert to track updates
  	orderTracking(
  		orderId: ID!
  	): Order!
  }
  1. Flow

    1. Add delivery personels to the system (addUser) 2 Order cookies from the store(placeOrder) and subscribe to order updates(orderTracking)
    2. Long polling by delivery personel client until order is available and delivery personel is free.
    3. The system v2 has a new clustering technique based on the latitude, longitude of the customer. The system divides the entire area in pseudo sections using angular geometry and any order belonging to the same section is attempted to be delivered together. But the system limits to 9 orders per section to decrease load on the delivery personel as well as map APIs. Each cluster is essentially a priority queue from which the orders are fetched on the basis of :
      • if orders arrived 10 mins apart each other then the order that came first should be served first else
      • The order closer to cookie shop (static (lat: latitude, long: longitude) => (19,19) => coordinates of the cookie shop)
    4. Notify the client about order that is sent out using publish subscriber model of apollo graphQL servers.
    5. Delivery perosnel scans the QR code that is given to customer and return the result to server for validation that cookie is delivered (verification of QR code to authenticate the presence of delivery personel on site).
    6. Client can enjoy cookies now!! And re-order them as well.
    7. Delivery Personel is now free and can again start long polling the server for new orders.
  2. Why long polling and not cron job with subscriptions?

Instead of wasting computational time on guessing when the delivery personel will be available, we make the delivery personel ask for new orders whenever he/she is ready. Drawback is this increases unnecessary new traffic that could be many times useless

Demo

  1. Add User(delivery Personel)
  2. Add User(customer)
  3. Login
  4. Place Order
  5. Delivery personels long polling for multiple orders
  6. Customers receiving real time ETA(client side subscription)
  7. Completing delivery
  8. Dashboards: a. Admin b. Personal Dashboard(Both customer and delivery personel)

Who do I talk to?

  • @NightWing1998 Mail Phone No: +91 9969326535/ 8850392965

About

Backend for cookie delivery applications. It can cluster orders based on location and distance from the shop. It also shows estimated time for delivery

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published