# foreplay

Last week was the busiest (most interesting) week I have ever been here. Why do you say so? Because the project needs to provide an interface for the server to summarize the user’s patient information and share the information in the form of email, we will investigate and deal with a set of implementation plans in a few days. Our server is implemented in Node.js (NPM has plenty of third-party libraries to spare you the embarrassment of rebuilding the wheel).

# # # – > HTML – PDF relation formula – > pdf2png – > s3 – > sendgrid

One of the trickier issues with developing pug is that the font is designed to work on the browser, but when taking web screenshots with phantom. Js (server-side JavaScript API WebKit) The font used the default font, and only custom font files solved this problem later.

style.
        @font-face {
            font-family: CustomFont;
            src: url('./views/fonts/HelveticaNeue.otf');
        }


        .gender {
            display: inline;
            color: rgb(102, 102, 102);
            font-family: CustomFont;
        }
Copy the code

Well, now that the template file is done, it’s time to implement the code.

Prepare data materials

Want to do certain thing affirmation can get the material that needs first hand first! (Feel programming and real life problem solving ideas are a lot of thought)

I am inspired by someone said through a summary list query, all the data N form through a query out, finally put it into the View, also convenient server-side development, I feel so a listen to is quite reasonable, do it for you (I feel like I’m execution competitive ☺)

In our system, some tables have fields that can be easily added to the field type, but when querying, we must consider row to column, or column to row (this was unknown territory before me).

select patient_id, max(IF(contact_type = 'email',value,null)) as 'email', max(IF(contact_type = 'address',value,null)) as 'address', group_concat(case when contact_type='mobile' then contacts.value end) as 'mobiles'
  from contacts where contacts.is_deleted = 0 group by patient_id
Copy the code

Max (IF(contact_type =’email’,value,null) can extract columns that match contact_type=’email’ as a single column; Group_concat (case when contact_type=’mobile’ then contacts.value end) can concatenate multiple types into a single cell. There was a problem, however, with not being able to differentiate multiple mobile devices. So we have to honestly query the data in batches in Node.js. Put a screenshot, you can definitely feel my mind a lot of XX horse (pay attention to the file name)

To explain the premise, customDiagnoses and patientDiagnoses tables are 1->N.

const customDiagnoses = await Promise.all(
            patientDiagnoses.map(item =>
                CustomDiagnosis.findById(item.sourceId)
            )
)
Copy the code

Promise.all() waits for all of the methods to be fully processed before executing the following statement

How to Turn an asynchronous method into a Seemingly synchronous method can be added here.

    static async exportPDF(html, options, filePath) {
        const promise = new Promise((reslove, reject) => {
            const callback = (err, res) => {
                if (err) {
                    reject(err)
                    return
                }
                reslove(res)
            }
            pdf.create(html, options).toFile(filePath, callback)
        })
        return promise
    }
Copy the code

Promise literally means that when you achieve something, I’ll do something for you. The most significant change to writing a Promise is the handling of the callback. Everything else is pretty much like this. It’s easy to use a Promise and just prefix the call with an await.

One might ask why Promise? Doesn’t Node.js always have callback? Why do I have to do it more than once?

It’s not always a case of wrapping asynchronous methods in Promise, but if you have N asynchronous &&s in a method that do the next thing based on the results of the previous method (then your code will be a ladder, one layer on top of the other, not easy to read).

Finally, AFTER my hard work, god was also moved and gave me a super cute cat. I also want to thank the so-and-so who supported and helped me in the development process!!

Next trailer, “Test master with flying, let you pretend to force you to blow”