Skip to content

Backend

basharhajjo edited this page Jun 1, 2025 · 8 revisions

This page keeps all information and steps to install and configure the Appwrite backend. All commands are for Linux only. If you wish to use another OS, you will have to adapt them to your OS.

Basic Concept

The backend of the Campus App serves as the main management unit for all processes the involve push notifications and data storage. As of right now, Appwrite is used as our backend software.

Upon startup of the app a new user account or session, depending on whether an account already exists, is created. Each account has a randomized email address serving as the user identifier and a randomized password. Both values are random numbers and cannot be traced back to any personal data.

The following features are currently backed by our backend:

  • Push notifications for saved events.
    • This is done by saving the Firebase Messaging token and the id of the event in the saved_events collection under the push_notifications database.
    • Upon startup the saved events are synchronized across the server and the app, which is done by adding locally saved events to the database and removing events that are no loner saved on the client side.
  • Configuration storage
    • Serving dynamic configuration attributes e.g. API URLs, update statuses, etc.

Installation

System requirements:

  • 1 CPU Core and 2GB of RAM
  • Docker
  • Docker Dompose Version 2

Follow the installation guide as instructed here: https://appwrite.io/docs/installation

In order to create the databases you also have to install the Appwrite CLI which can be found here: https://appwrite.io/docs/command-line

After setup, make sure to follow the Environment Variables section, otherwise you won't be able to communicate with your own backend


Configuration

After you have installed the backend, you have to create your first project with a unique project id.


Databases

To create the required databases for the campus app, you have to run the following commands:

Push Notifications

  • Create the database:
    • appwrite databases create --databaseId push_notifications --name 'Push Notifications'

Saved Events

  1. Create the saved events collection:

    • appwrite databases createCollection --databaseId push_notifications --collectionId saved_events --name 'Saved Events' --permissions 'create("users")' --documentSecurity true
  2. Create the following attributes for the saved_events collection:

    • String fcmToken : appwrite databases createStringAttribute --databaseId push_notifications --collectionId saved_events --key fcmToken --size 250 --required true
    • int eventId : appwrite databases createIntegerAttribute --databaseId push_notifications --collectionId saved_events --key eventId --required true
    • String startDate : appwrite databases createStringAttribute --databaseId push_notifications --collectionId saved_events --key startDate --size 30 --required true
  3. Create an index for the saved_events collection:

    • appwrite databases createIndex --databaseId push_notifications --collectionId saved_events --key index --type key --attributes 'fcmToken' --orders 'ASC'

Accounts

  • Create the accounts database:
    • appwrite databases create --databaseId accounts --name 'Accounts'

Last Login Collection:

  1. Create the last_login collection:

    • appwrite databases createCollection --databaseId accounts --collectionId last_login --name 'Last Login' --permissions 'create("users")' --documentSecurity true
  2. Create the following attributes for the last_login collection:

    • String userId : appwrite databases createStringAttribute --databaseId accounts --collectionId last_login --key userId --size 25 --required true
    • String date : appwrite databases createStringAttribute --databaseId accounts --collectionId last_login --key date --size 30 --required true
  3. Create an index for the last_login collection:

    • appwrite databases createIndex --databaseId accounts --collectionId last_login --key index --type key --attributes 'userId' --orders 'ASC'

Data

  • Create the data database:
    • appwrite databases create --databaseId data --name 'Data'

Publishers

  1. Create the publishers collection:

    • appwrite databases createCollection --databaseId data --collectionId publishers --name 'Publishers' --permissions 'read("any")'
  2. Create the following attributes for the publishers collection:

    • String wpUserId : appwrite databases createIntegerAttribute --databaseId data --collectionId publishers --key id --required true
    • String name : appwrite databases createStringAttribute --databaseId data --collectionId publishers --key name --size 100 --required true
    • String initiallyDisplayed : appwrite databases createBooleanAttribute --databaseId data --collectionId publishers --key initiallyDisplayed --xdefault false --required false
    • bool hidden : appwrite databases createBooleanAttribute --databaseId data --collectionId publishers --key hidden --xdefault false --required false
  3. Create an index for the publishers collection:

    • appwrite databases createIndex --databaseId data --collectionId publishers --key index --type key --attributes 'id' --orders 'ASC'

Study courses

  1. Create the study_courses collection:

    • appwrite databases createCollection --databaseId data --collectionId study_courses --name 'Studiengänge' --permissions 'read("any")'
  2. Create the following attributes for the study_courses collection:

    • String wpUserId : appwrite databases createIntegerAttribute --databaseId data --collectionId study_courses --key pId --required true
    • String name : appwrite databases createStringAttribute --databaseId data --collectionId study_courses --key name --size 100 --required true
    • String faculty : appwrite databases createStringAttribute --databaseId data --collectionId study_courses --key faculty --size 220 --required true
  3. Create an index for the study_courses collection:

    • appwrite databases createIndex --databaseId data --collectionId study_courses --key index --type key --attributes 'pId' --orders 'ASC'

Here's the complete Appwrite backend documentation section for your coupons system, formatted exactly like your original document for easy copy-pasting:

Coupons

  • Create the database:
    • appwrite databases create --databaseId coupons --name 'Coupons'

couponUsers Collection

  1. Create the collection:

    • appwrite databases createCollection --databaseId coupons --collectionId couponUsers --name 'CouponUsers' --permissions 'create("users")' --documentSecurity true
  2. Create the attributes:

    • (List of Strings) favoriteCoupons: appwrite databases createStringAttribute --databaseId coupons --collectionId couponUsers --key favoriteCoupons --size 49 --required false --array true
    • (List of Strings) likedCoupons: appwrite databases createStringAttribute --databaseId coupons --collectionId couponUsers --key likedCoupons --size 49 --required false --array true
    • (List of Strings) dislikedCoupons: appwrite databases createStringAttribute --databaseId coupons --collectionId couponUsers --key dislikedCoupons --size 49 --required false --array true
    • (List of Strings) userMaxCoupons: appwrite databases createStringAttribute --databaseId coupons --collectionId couponUsers --key userMaxCoupons --size 49 --required false --array true
  3. Create an index:

    • appwrite databases createIndex --databaseId coupons --collectionId couponUsers --key userIndex --type key --attributes 'userId' --orders 'ASC'

coupons Collection

  1. Create the collection:

    • appwrite databases createCollection --databaseId coupons --collectionId coupons --name 'Coupons' --permissions 'read("any")'
  2. Create the attributes:

    • String title: appwrite databases createStringAttribute --databaseId coupons --collectionId coupons --key title --size 50 --required true
    • String description: appwrite databases createStringAttribute --databaseId coupons --collectionId coupons --key description --size 99999 --required true
    • String provider: appwrite databases createStringAttribute --databaseId coupons --collectionId coupons --key provider --size 50 --required true
    • String discount: appwrite databases createStringAttribute --databaseId coupons --collectionId coupons --key discount --size 25 --required false --xdefault null
    • Float oldPrice: appwrite databases createFloatAttribute --databaseId coupons --collectionId coupons --key oldPrice --required false --min 0 --max 9999 --xdefault null
    • Float newPrice: appwrite databases createFloatAttribute --databaseId coupons --collectionId coupons --key newPrice --required false --min 0 --max 9999 --xdefault null
    • Url website: appwrite databases createUrlAttribute --databaseId coupons --collectionId coupons --key website --required false --xdefault null
    • Url location: appwrite databases createUrlAttribute --databaseId coupons --collectionId coupons --key location --required false --xdefault null
    • Enum category: appwrite databases createEnumAttribute --databaseId coupons --collectionId coupons --key category --elements Essen Technik Mode Reisen Sonstiges --required true
    • DateTime expiresAt: appwrite databases createDatetimeAttribute --databaseId coupons --collectionId coupons --key expiresAt --required false --xdefault null
    • String qrCode: appwrite databases createStringAttribute --databaseId coupons --collectionId coupons --key qrCode --size 999 --required false --xdefault null
    • String hiddenQrCode: appwrite databases createStringAttribute --databaseId coupons --collectionId coupons --key hiddenQrCode --size 999 --required false --xdefault null
    • Integer availableCoupons: appwrite databases createIntegerAttribute --databaseId coupons --collectionId coupons --key availableCoupons --required false --min 0 --max 999999 --xdefault null
    • Integer couponUsesCounter: appwrite databases createIntegerAttribute --databaseId coupons --collectionId coupons --key couponUsesCounter --required false --min 0 --max 999999 --xdefault 0
    • Integer voteCount: appwrite databases createIntegerAttribute --databaseId coupons --collectionId coupons --key voteCount --required false --min -999999 --max 999999 --xdefault 0
    • (array of Urls) images : appwrite databases createStringAttribute --databaseId coupons --collectionId coupons --key images --size 2048 --required false --array true
  3. Create indexes:

    • appwrite databases createIndex --databaseId coupons --collectionId coupons --key titleIndex --type fulltext --attributes 'title'
    • appwrite databases createIndex --databaseId coupons --collectionId coupons --key providerIndex --type key --attributes 'provider' --orders 'ASC'
    • appwrite databases createIndex --databaseId coupons --collectionId coupons --key categoryIndex --type key --attributes 'category' --orders 'ASC'

Cloud Functions

Cloud functions can be written in a variety of programming language such as Python or Javascript. We chose JavaScript, powered by NodeJS to run our cloud functions.

Saved Event Push Notifications

  1. Create the respective Cloud Function:

    • appwrite functions create --functionId push_saved_events --name 'Saved Event Push Notifications' --runtime node-16.0 --execute users
  2. Create an API key and set the API_KEY secret as a function variable using the Appwrite console webinterface.

  3. Create a deployment through the web interface or run the following command (Replace the path with your path to the savedEvents cloud function folder from our cloud functions repository:

    • appwrite functions createDeployment --functionId push_saved_events --activate=true --entrypoint="savedEventPushNotification.js"--code aw-functions/push-notifications/savedEventsNotification/

Create User

  1. Create the respective Cloud Function:

    • appwrite functions create --functionId create_user --name 'Create User' --runtime node-16.0 --execute any
  2. Add your Appwrite API key as as a function variable, while using the API_KEY identifier.

  3. Generate a random string and set it as a function variable with the AUTH_KEY identifier.

  4. Create a deployment through the web interface or run the following command:

    • appwrite functions createDeployment --functionId create_user --activate=true --entrypoint="createUser.js" --code aw-functions/users/createUser/

General Push Notifications

  1. Create the respective Cloud Function:

    • appwrite functions create --functionId push_general --name 'General Push Notifications' --runtime node-16.0
  2. Generate a random string and set it as a function variable with the AUTH_KEY identifier.

  3. Create a deployment through the web interface or run the following command:

    • appwrite functions createDeployment --functionId push_general --activate=true --entrypoint="sendPushNotification.js" --code aw-functions/push-notifications/generalNotification

Miscellaneous commands:

  • Delete deployment
    • appwrite functions deleteDeployment --deploymentId <Deployment-ID> --functionId <Function-ID>