boolean-uk / team-dev-server

3 stars 12 forks source link

As a developer, so I can get visibility on the activity of the application, I want significant events users take to be shown to be in a dashboard#61 #270

Closed ning905 closed 1 year ago

ning905 commented 1 year ago

1- Add a DEVELOPER role in enum Role.

2- Create an Event model in the DB.

Screenshot 2022-09-28 at 10 19 18

Both createdBy and receivedBy are users, and createdBy is nullable. For example, if a person registers as a user, the receivedById will be that user’s id. If a teacher assigns this user to a cohort, the receivedById will be that user’s id and the createdById be that teacher’s id.

3- Modify functions related to significant events (e.g. when a user gets created, when a user’s role is changed, when an error happens, when a user is assigned to a cohort) to create a respective event at the same time.

4- Create a route and a controller for ‘/events’ and a getEvents function in the controller. When the server receives a GET request on this route, run the validateAuthentication check first. If it fails, return an error message respectively.

5- If the request passes the validation state, the request object should have a user property, where we could check their role. If the role is not DEVELOPER, return error 403, ‘Only developers can see this data’. If the role is DEVELOPER, run a findMany query on the DB, sorted by the createdAt time from the most recent to the oldest, and return 200 with the data.

vherus commented 1 year ago

This is a really great start. How are you going to trigger events and handle them? I recommend you and the whole team spend time researching the node EventEmitter, here's an intro document: https://www.section.io/engineering-education/guide-to-nodejs-event-emitter/

Essentially, it works like this:

  1. User creates an account, the createUser function runs
  2. That function creates a user, and then emits an event called user-created
  3. The node EventEmitter sees that an event with the name user-created was emitted, and runs a function to perform some logic to handle extra stuff that needs to happen when that event happens (e.g. adding something to the Events db table)

Go do some research, watch some videos, read some documentation and practice using the event emitter in a js file that you can run with node, like node test.js then come back here with an updated plan of action about how you're going to use it. I'd like to see at least one code example for one of the actual events in the app.

Additionally, we'll need to see API response examples for the GET route

ning905 commented 1 year ago

Use Node.js Event Emitter. https://www.section.io/engineering-education/guide-to-nodejs-event-emitter/

const EventEmitter = require(`events`);
class MyEventEmitter extends EventEmitter{}
const myEmitter = new MyEventEmitter();

Register listeners with keywords(e.g. user-created) and callback functions to trigger creating an event in the DB.

myEventEmitter.on(‘keyword’, () => {callback()});

Insert myEmitter.emit(‘keyword’) into significant functions, such as creating a user, assigning new roles, and assigning a student to a cohort.

When creating an event record, the keyword triggering myEmitter will be recorded as topic and additional information (e.g. error code and error message) will be content. Event model:

enum EventType {
  ERROR
  USER
  ADMIN
  COHORT
}

model Event {
  id           Int       @id @default(autoincrement())
  type         EventType
  topic        String?
  content      String?
  createdBy    User?     @relation("createdEvents", fields: [createdById], references: [id], onDelete: Cascade)
  createdById  Int?
  receivedBy   User?      @relation("receivedEvents", fields: [receivedById], references: [id], onDelete: Cascade)
  receivedById Int?
  cohort       Cohort?   @relation(fields: [cohortId], references: [id], onDelete: Cascade)
  cohortId     Int?
  createdAt    DateTime  @default(now())
  updatedAt    DateTime? @updatedAt
}

API response examples for the GET route

[
  {
    id: 1,
    type: 'USER',
    topic: 'registration ',
    receivedById: 1,
    createdAt: 2022-09-28T15:56:45.683Z,
    updatedAt: 2022-09-28T15:56:45.684Z
  },
  {
    id: 2,
    type: 'COHORT',
    topic: 'created ',
    createdById: 1,
    cohortId: 1, 
    createdAt: 2022-09-28T15:56:45.766Z,
    updatedAt: 2022-09-28T15:56:45.767Z
  }
]
Auenc commented 1 year ago

Looks good to me, great work!