Javascript RPC Call Handles Data and Returns DB Response

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have a service within a larger set of microservices that's handling a RPC call. My function mainly just parses through the request, uses the data to insert data into our MongoDB database, and then returns the data that got stored into the database (should be idempotent).



Endpoint Function



type GAPFType = 
facultyId: number,
created: number,
lastModified: number,
status: Status,
attachedDocuments: Array<DocumentType>
;

type CallbackType =
error: ?string,
payload: ?GAPFType
;

const submitGAPFBackend = async (
gapfRequest: GAPFType
): Promise<CallbackType> =>
logger.info("Enter submitGAPFBackend with request body %j", gapfRequest);

const facultyId = gapfRequest;
if (facultyId === undefined)
logger.error("facultyId missing from request body: %j", gapfRequest);
return invalidSubmitGAPFCallError;


const gapf = createSubmitGAPFObject(gapfRequest);
logger.info("Request GAPF object: %j", gapf);

try
const submittedForm = await GAPFApplication.submit(gapf);
const submittedData = getSubmittedGAPF(submittedForm);
logger.info("DB response with filtered data: %j", submittedData);

const payload = GAPF.create(submittedData);
logger.info("Response payload data: %j", payload);
return error: null, payload: payload ;
catch (error)
logger.error("Error: %j", error.message);
return error: error, payload: null ;

;


Helper Functions



export const createSubmitGAPFObject = gapfRequest => 
const now = getUNIXTimestamp();
const
facultyId,
status = "SUBMITTED",
created = now,
attachedDocuments = []
= gapfRequest;

return
facultyId: facultyId,
created: created,
lastModified: now,
status: status,
attachedDocuments: attachedDocuments
;
;

export const getSubmittedGAPF = submittedForm =>
return
facultyId: submittedForm.facultyId,
created: submittedForm.created,
lastModified: submittedForm.lastModified,
status: submittedForm.status,
attachedDocuments: submittedForm.attachedDocuments.map(doc =>
// removing _id from document data
return
name: doc.name,
link: doc.link,
attachedDate: doc.attachedDate
;
)
;
;

export const invalidSubmitGAPFCallError =
error:
message: "facultyId missing from request body",
status: grpc.status.INVALID_ARGUMENT
,
payload: null
;

export const getUNIXTimestamp = () =>
return Math.floor(Date.now() / 1000);
;


I don't program in Javascript often, so I was hoping to get some feedback on writing cleaner or more idiomatic code. The original code has docstrings, but I've left those out for brevity. And the type annotations are from flow.







share|improve this question



























    up vote
    2
    down vote

    favorite












    I have a service within a larger set of microservices that's handling a RPC call. My function mainly just parses through the request, uses the data to insert data into our MongoDB database, and then returns the data that got stored into the database (should be idempotent).



    Endpoint Function



    type GAPFType = 
    facultyId: number,
    created: number,
    lastModified: number,
    status: Status,
    attachedDocuments: Array<DocumentType>
    ;

    type CallbackType =
    error: ?string,
    payload: ?GAPFType
    ;

    const submitGAPFBackend = async (
    gapfRequest: GAPFType
    ): Promise<CallbackType> =>
    logger.info("Enter submitGAPFBackend with request body %j", gapfRequest);

    const facultyId = gapfRequest;
    if (facultyId === undefined)
    logger.error("facultyId missing from request body: %j", gapfRequest);
    return invalidSubmitGAPFCallError;


    const gapf = createSubmitGAPFObject(gapfRequest);
    logger.info("Request GAPF object: %j", gapf);

    try
    const submittedForm = await GAPFApplication.submit(gapf);
    const submittedData = getSubmittedGAPF(submittedForm);
    logger.info("DB response with filtered data: %j", submittedData);

    const payload = GAPF.create(submittedData);
    logger.info("Response payload data: %j", payload);
    return error: null, payload: payload ;
    catch (error)
    logger.error("Error: %j", error.message);
    return error: error, payload: null ;

    ;


    Helper Functions



    export const createSubmitGAPFObject = gapfRequest => 
    const now = getUNIXTimestamp();
    const
    facultyId,
    status = "SUBMITTED",
    created = now,
    attachedDocuments = []
    = gapfRequest;

    return
    facultyId: facultyId,
    created: created,
    lastModified: now,
    status: status,
    attachedDocuments: attachedDocuments
    ;
    ;

    export const getSubmittedGAPF = submittedForm =>
    return
    facultyId: submittedForm.facultyId,
    created: submittedForm.created,
    lastModified: submittedForm.lastModified,
    status: submittedForm.status,
    attachedDocuments: submittedForm.attachedDocuments.map(doc =>
    // removing _id from document data
    return
    name: doc.name,
    link: doc.link,
    attachedDate: doc.attachedDate
    ;
    )
    ;
    ;

    export const invalidSubmitGAPFCallError =
    error:
    message: "facultyId missing from request body",
    status: grpc.status.INVALID_ARGUMENT
    ,
    payload: null
    ;

    export const getUNIXTimestamp = () =>
    return Math.floor(Date.now() / 1000);
    ;


    I don't program in Javascript often, so I was hoping to get some feedback on writing cleaner or more idiomatic code. The original code has docstrings, but I've left those out for brevity. And the type annotations are from flow.







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a service within a larger set of microservices that's handling a RPC call. My function mainly just parses through the request, uses the data to insert data into our MongoDB database, and then returns the data that got stored into the database (should be idempotent).



      Endpoint Function



      type GAPFType = 
      facultyId: number,
      created: number,
      lastModified: number,
      status: Status,
      attachedDocuments: Array<DocumentType>
      ;

      type CallbackType =
      error: ?string,
      payload: ?GAPFType
      ;

      const submitGAPFBackend = async (
      gapfRequest: GAPFType
      ): Promise<CallbackType> =>
      logger.info("Enter submitGAPFBackend with request body %j", gapfRequest);

      const facultyId = gapfRequest;
      if (facultyId === undefined)
      logger.error("facultyId missing from request body: %j", gapfRequest);
      return invalidSubmitGAPFCallError;


      const gapf = createSubmitGAPFObject(gapfRequest);
      logger.info("Request GAPF object: %j", gapf);

      try
      const submittedForm = await GAPFApplication.submit(gapf);
      const submittedData = getSubmittedGAPF(submittedForm);
      logger.info("DB response with filtered data: %j", submittedData);

      const payload = GAPF.create(submittedData);
      logger.info("Response payload data: %j", payload);
      return error: null, payload: payload ;
      catch (error)
      logger.error("Error: %j", error.message);
      return error: error, payload: null ;

      ;


      Helper Functions



      export const createSubmitGAPFObject = gapfRequest => 
      const now = getUNIXTimestamp();
      const
      facultyId,
      status = "SUBMITTED",
      created = now,
      attachedDocuments = []
      = gapfRequest;

      return
      facultyId: facultyId,
      created: created,
      lastModified: now,
      status: status,
      attachedDocuments: attachedDocuments
      ;
      ;

      export const getSubmittedGAPF = submittedForm =>
      return
      facultyId: submittedForm.facultyId,
      created: submittedForm.created,
      lastModified: submittedForm.lastModified,
      status: submittedForm.status,
      attachedDocuments: submittedForm.attachedDocuments.map(doc =>
      // removing _id from document data
      return
      name: doc.name,
      link: doc.link,
      attachedDate: doc.attachedDate
      ;
      )
      ;
      ;

      export const invalidSubmitGAPFCallError =
      error:
      message: "facultyId missing from request body",
      status: grpc.status.INVALID_ARGUMENT
      ,
      payload: null
      ;

      export const getUNIXTimestamp = () =>
      return Math.floor(Date.now() / 1000);
      ;


      I don't program in Javascript often, so I was hoping to get some feedback on writing cleaner or more idiomatic code. The original code has docstrings, but I've left those out for brevity. And the type annotations are from flow.







      share|improve this question













      I have a service within a larger set of microservices that's handling a RPC call. My function mainly just parses through the request, uses the data to insert data into our MongoDB database, and then returns the data that got stored into the database (should be idempotent).



      Endpoint Function



      type GAPFType = 
      facultyId: number,
      created: number,
      lastModified: number,
      status: Status,
      attachedDocuments: Array<DocumentType>
      ;

      type CallbackType =
      error: ?string,
      payload: ?GAPFType
      ;

      const submitGAPFBackend = async (
      gapfRequest: GAPFType
      ): Promise<CallbackType> =>
      logger.info("Enter submitGAPFBackend with request body %j", gapfRequest);

      const facultyId = gapfRequest;
      if (facultyId === undefined)
      logger.error("facultyId missing from request body: %j", gapfRequest);
      return invalidSubmitGAPFCallError;


      const gapf = createSubmitGAPFObject(gapfRequest);
      logger.info("Request GAPF object: %j", gapf);

      try
      const submittedForm = await GAPFApplication.submit(gapf);
      const submittedData = getSubmittedGAPF(submittedForm);
      logger.info("DB response with filtered data: %j", submittedData);

      const payload = GAPF.create(submittedData);
      logger.info("Response payload data: %j", payload);
      return error: null, payload: payload ;
      catch (error)
      logger.error("Error: %j", error.message);
      return error: error, payload: null ;

      ;


      Helper Functions



      export const createSubmitGAPFObject = gapfRequest => 
      const now = getUNIXTimestamp();
      const
      facultyId,
      status = "SUBMITTED",
      created = now,
      attachedDocuments = []
      = gapfRequest;

      return
      facultyId: facultyId,
      created: created,
      lastModified: now,
      status: status,
      attachedDocuments: attachedDocuments
      ;
      ;

      export const getSubmittedGAPF = submittedForm =>
      return
      facultyId: submittedForm.facultyId,
      created: submittedForm.created,
      lastModified: submittedForm.lastModified,
      status: submittedForm.status,
      attachedDocuments: submittedForm.attachedDocuments.map(doc =>
      // removing _id from document data
      return
      name: doc.name,
      link: doc.link,
      attachedDate: doc.attachedDate
      ;
      )
      ;
      ;

      export const invalidSubmitGAPFCallError =
      error:
      message: "facultyId missing from request body",
      status: grpc.status.INVALID_ARGUMENT
      ,
      payload: null
      ;

      export const getUNIXTimestamp = () =>
      return Math.floor(Date.now() / 1000);
      ;


      I don't program in Javascript often, so I was hoping to get some feedback on writing cleaner or more idiomatic code. The original code has docstrings, but I've left those out for brevity. And the type annotations are from flow.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Mar 20 at 3:41
























      asked Mar 20 at 3:34









      abrarisme

      1639




      1639

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189996%2fjavascript-rpc-call-handles-data-and-returns-db-response%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f189996%2fjavascript-rpc-call-handles-data-and-returns-db-response%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation