Real-Dev-Squad / website-backend

The backend code for all our website-related apps
https://api.realdevsquad.com/
MIT License
53 stars 257 forks source link

[RFC] Transformation Layer #1493

Open pallabez opened 1 year ago

pallabez commented 1 year ago

Problem

Currently we are fetching data from many external services (firestore, github, discord service). We mostly change a few fields of the data according to our needs and send it to controller and eventually as API response.

A few problem arises with this -

Transformation of data happening at different places

/controller/auth.js

      userData = {
        github_id: user.username,
        github_display_name: user.displayName,
        github_created_at: Number(new Date(user._json.created_at).getTime()),
        created_at: Date.now(),
        updated_at: Date.now(),
      };

      const { userId, incompleteUserDetails } = await users.addOrUpdate(userData);

/models/discordactions.js

    return groups.map((group) => {
      const groupCreator = groupCreatorsDetails[group.createdBy];
      return {
        ...group,
        firstName: groupCreator?.first_name,
        lastName: groupCreator?.last_name,
        image: groupCreator?.picture?.url,
        memberCount: roleIdToCountMap[group.roleid] || 0, // Number of users joined this group
        isMember: subscribedGroupIds.has(group.roleid), // Is current loggedIn user is a member of this group
      };
    });

No reliable way to ensure no sensitive data is sent.

We mostly return the data object to the frontend as we are given by firestore. Private fields need to be handled with more conditions throughout the codebase.

The below solution helps but is not reliable as - New private fields can be added to user as well as at other models.

/services/dataAccessLayer.js

const removeSensitiveInfo = function (obj, level = ACCESS_LEVEL.PUBLIC) {
  for (let i = 0; i < KEYS_NOT_ALLOWED[level].length; i++) {
    if (Object.prototype.hasOwnProperty.call(obj, KEYS_NOT_ALLOWED[level][i])) {
      delete obj[KEYS_NOT_ALLOWED[level][i]];
    }
  }
};

No fixed place to do date parsing or other parsing

/models/extensionRequest.js

    const request = {
      timestamp: Number((new Date().getTime() / 1000).toFixed(0)),
      ...extensionRequestData,
    };
    return await extensionRequestsModel.add(request);

Proposed Solution

Architecture drawio(1)

Benifits

Demerits

Migration Plan

Alternate Solution

It can be called serializer instead. Ember data serializer does this functionality as mentioned here
(To be discussed more)

iamitprakash commented 1 year ago

@pallabez please make changes as discussed

iamitprakash commented 1 year ago

@pallabez need role based access control over db tables