This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": "objectType-23423423-234234-234234-23423", | |
"data": {...}, | |
"createdAt": 4564564564, | |
"updatedAt": 4564564567, | |
"createdBy": 'user-630eb68f-e0fa-5ecc-887a-7c7a62614681', //optional | |
"deletedAt": 4565961589 // optional | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { v1 as uuidv1 } from 'uuid'; | |
function uuid(identifier){ | |
return identifier + '-' + uuidv1(); | |
} | |
uuid('user'); // ⇨ 'user-9k1teb4d-5b7d-7b2d-9bdd-2b0d7b3dcb6d' | |
uuid('product'); // ⇨ 'product-6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// USER | |
{ | |
"id": "user-23423423-234234-234234-23423", | |
"data": { | |
"firstname": "John", | |
"lastname": "Doe", | |
"email": "john.doe@gmail.com", | |
"age": "32" | |
}, | |
"createdAt": 7124564538, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// automatically appends updatedAt timestamp | |
const update = async (updateParams) => { | |
try { | |
const timestamp = new Date().getTime(); | |
params.UpdateExpression = params.UpdateExpression + ', #updatedAt = :updatedAt'; | |
params.ExpressionAttributeNames['#updatedAt'] = 'updatedAt'; | |
params.ExpressionAttributeValues[':updatedAt'] = timestamp; | |
return await db.update(updateParams).promise(); | |
} | |
catch (error) { throw error; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jwt = require('jsonwebtoken'); | |
// extract token from header | |
let authHeader = request.header.Authorization; | |
let token = authHeader.split(" ")[1]; | |
// decode token and get user's 'role' | |
let decodedVal = jwt.verify(token, process.env.JWT_KEY); | |
let role = decodedVal.role; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PERMISSIONS = { | |
"vendor": { | |
"products": ["POST", "PUT", "DELETE", "GET"], | |
"orders": ["POST", "PUT", "DELETE", "GET"], | |
"stores": ["POST", "PUT", "DELETE", "GET"], | |
"dashboard": ["GET"] | |
}, | |
"customer": { | |
"products": ["GET"], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkPermission(role, resource, httpVerb){ | |
if (PERMISSIONS[role] && PERMISSIONS[role][resource]) | |
return PERMISSIONS[role][resource].includes(httpVerb); | |
return false; | |
} | |
// Example | |
// request from "admin" | |
// POST https://test-domain.com/products/ => true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let data = []; | |
try { | |
let query = { | |
index: 'index-name', | |
body: { | |
query: { | |
bool: { | |
// ... | |
} | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const app = express() | |
const port = 3000 | |
const { requestCode, verifyOtp, cancelOtp } = require("./vonage.service.js"); | |
app.post('/api/request-token', (req, res) => { | |
//... | |
}) | |
app.post('/api/verify-token', (req, res) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Vonage = require('@vonage/server-sdk'); | |
const vonage = new Vonage({ | |
apiKey: process.env.VONAGE_KEY, | |
apiSecret: process.env.VONAGE_SECRET | |
}); | |
export const requestCode = (phoneNo, callback) => { | |
//... | |
}); |