Part 12: Backoffice and server refactor
Introduction
This guide aims to program a website to accept Bitcoin. In the last tutorial (part 11), we re-coded the way we generated and stored or cold storage generated addresses. In this part, we are going to put in place the framework to deal with the back office functionality (sending emails for example). This involves a decent refactor of the “server” component of the tutorial.
Also from this part onwards, we will not be listing the code in its entirety, mainly because a lot of work will be small bug fixes and refactoring of pieces of code that we clearly covered in previous tutorials. Instead, we will offer top-level explanations of the changes and deep dive new code and concepts that we have implemented.
The SQL
We finally got around to renaming the horribly names keys table to something that makes sense, we renamed it to sessions.
We also added a new table called emailtemplates to handle the various emails that the system will be sending out.
CREATE TABLE "emailtemplates"
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` TEXT,
`subject` TEXT,
`body` TEXT,
`fromname` TEXT,
`fromemail` TEXT,
`active` INTEGER DEFAULT 1
)
The Code
As previously stated all the code we did this time is in the “server” section and involved a decent refactor. The reason for this refactoring was, simply “app.js” was getting very large and doing multiple things that were technically outside its purview. We changed it to simply handle “routing” and the logic for these routes has been moved to 4 helpers, one for each major endpoint and a generic one for reusable code such as the send email function.
Note, we have kept the code as simple and uniform as possible and refrained from using some of the more complex methodologies such as “async” and “await” and such to make the code as accessible as possible.
admin helper
This helper contains all of the admin functions. They are functions required for the “admin control panel” to work.
api helper
This helper contains all of the API functions that are required for the “CDN” to work.
backoffice helper
This helper will contain the back office functions such as payment monitor. These are functions that rather wait on user input as generally ran on a timer.
generic helper
This helper holds all of the generic functions that the rest of the code base uses such as sending emails.
Regression Testing
As we were refactoring this code we noticed (and fixed) a number of bugs. This lead to believe it was time for some (simple) “regression testing”.
There are many ways to do this including “unit tests”, “headless browsers” and “static code analysis”. We may look into these in the future but for now simply running through them and putting a tick in a column on a spreadsheet is good enough. Included is a simple regression testing spreadsheet which you can find “here”.
The video below shows a full regression test.
Conclusion
So there we are, nice little update, bugs were fixed, the code was refactored and we have a fully regressed application.
Next time we will work on the CDN part and add real-time notifications as well as the ability pass in additional view to the cart (shipping/billing address etc) and store them in a way in the database that requires no more work in the future (maybe)