# Pre-interview question(Deprecation)
We have created this exercise to gain insights into your development skills.
## What to do?
There are two parts to the questions: The first part is about your coding skill. The second part is about your knowledge.
## Coding
- [Routing](https://hackmd.io/@Aigniter/Sk42DKyxY)
## Short questions
The questions below may contain 1 or more syntax error(s), logical error(s) or security issue(s). Please point them out as many as you can.
1.
```javascript=
// Purpose: given a list of users and return a list of VIP member users
function filterAccount(userList) {
userList.filter(async (user) => {
const isMember = await checkIsVipMember(user.id); //an async function that checks the user is a VIP member.
return isMember;
});
return userList;
}
```
2.
```javascript=
const { v4: uuid4 } = require('uuid'); //https://www.npmjs.com/package/uuid
const NODE_ENV = process.env.NODE_ENV || "development";
const knexFile = require("../knexfile")[NODE_ENV]
const knex = require('knex')(knexFile); //https://www.npmjs.com/package/knex
//...
// The function `randomString` generates a random string
function randomString(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
// Purpose: Create a new user
async function createUser(username, email, name, password) {
//The function `validation` contains some operations which checking the input email, name, username, and password are valid and do not exist in the current database, throw an error if any field is invalid. Assume this function has no error.
await validation('create_user', {
username,
email,
name,
password
});
const salt = randomString(16); // Generate a random string in 16 chars and use it as `salt`.
// Now it inserts the `user` to `users` table.
const user = await knex('users').insert({
id: uuid4(),
username: username,
email: email,
name: name,
password: btoa(password + salt), //https://developer.mozilla.org/en-US/docs/Web/API/btoa
salt: salt
});
return user.id;
}
```
3.
```javascript=
const NODE_ENV = process.env.NODE_ENV || "development";
const knexFile = require("../knexfile")[NODE_ENV]
const knex = require('knex')(knexFile); //https://www.npmjs.com/package/knex
// const Exception = require('./Exception.js');
const passwordUtils = require('./passwordUtils'); // a utils for password handling
// Purpose: Delete user
async function deleteUser(id, password) {
try {
// It checks user password is correct.
// The return object is an array which includes the data and some metadata, we just need the data part so we just take the first part of the array.
const passwordInfo = (
await knex.raw("SELECT password FROM user where `id` = '" + id + "'")
)[0];
// console.log(passwordInfo);
if (passwordInfo.length) {
const passwordHash = passwordInfo[0].password;
const passwordCheck = await passwordUtils.verify(passwordHash, password);
if (!passwordCheck) {
throw new Error("Incorrect password");
}
}
// It checks the user has any existing uncompleted orders.
// The return object is an array which includes the data and some metadata, we just need the data part so we just take the first part of the array.
const existingProcessingOrder = (
await knex.raw(
"SELECT COUNT(*) AS count FROM order_history where `user_id` = '" +
id +
"' AND `status` = 'processing'"
)
)[0];
console.log(existingProcessingOrder);
if (
existingProcessingOrder[0].count > 0
) {
throw new Error(`The user ${id} exists uncompleted orders`);
} else {
await this.knex("users").where({ id: id }).del();
}
} catch (err) {
console.log(err);
throw new Error("Internal server error");
}
}
```
- Answer all 3 questions in `short_questions.txt`.
## Submission
- Zip `p1.js` or `p1.ts` and `short_questions.txt` to `answer_<candidate_name>.zip`, e.g. `answer_Siu_Ming_Chan.zip`
- Reply this email before the deadline and attach the `answer_<candidate_name>.zip`
## Problems?
Feel free to contact us if something is not clear or you encounter any difficulties.