Skip to content

Instantly share code, notes, and snippets.

View hoangsetup's full-sized avatar
🎯
Work until you no longer have to represent yourself.!

Hoang Dinh hoangsetup

🎯
Work until you no longer have to represent yourself.!
View GitHub Profile
diff --git a/docker-compose.yml b/docker-compose.yml
index 94302ff..19dff7a 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,7 +1,7 @@
version: '3.9'
services:
api:
- build: .
+ build: node:latest
const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const router = express.Router();
const config = require('./config');
const utils = require('./utils');
const tokenList = {};
const app = express();
router.get('/', (req, res) => {
@hoangsetup
hoangsetup / app.ts
Last active September 20, 2021 08:28
// src/app.ts
import express, { Router } from 'express';
import callStateController from './controllers/call.state.controller';
import * as path from 'path';
const app = express();
app.use(express.json());
app.get('/', (req, res) => {
res.sendFile(path.join(process.env.PUBLIC_DIR as string, 'index.html'));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tiny CRM</title>
<style>
body {
padding: 10px;
font-size: 18px;
}
// src/call-state.controller.ts
import { Request, Response } from 'express';
import CustomerModel from '../models/customer.model';
import CallStateModel from '../models/call.state.model';
import { emitCallStateChange } from '../socket';
import moment from 'moment';
const callStateController = {
handleNewCallState: async (req: Request, res: Response) => {
try {
@hoangsetup
hoangsetup / server.ts
Last active September 20, 2021 07:54
// src/server.ts
import * as http from 'http';
import dotenv from 'dotenv';
import app from './app';
import mongoose from 'mongoose';
import { initSocketIo } from './socket';
(async () => {
dotenv.config();
@hoangsetup
hoangsetup / socket.ts
Last active September 20, 2021 07:52
// src/socket.ts
import { Server } from 'socket.io';
import * as http from 'http';
let io: Server;
export function initSocketIo(server: http.Server) {
if (io) {
return;
}
// src/controllers/call-state.controller.ts
import { Request, Response } from 'express';
import CustomerModel from '../models/customer.model';
import CallStateModel from '../models/call.state.model';
const callStateController = {
handleNewCallState: async (req: Request, res: Response) => {
try {
const { phone_number: phoneNumber, state, timestamp } = req.body;
// src/models/call-state.model.ts
import { Schema, Model, model, ObjectId } from 'mongoose';
import CustomerModel from './customer.model';
export enum CallState {
RINGING = 'RINGING',
OFFHOOK = 'OFFHOOK',
IDLE = 'IDLE',
}
// src/models/customer.model.ts
import { Schema, Model, model } from 'mongoose';
export interface ICustomer {
name: string;
phoneNumber: string;
}
const CustomerSchema = new Schema<ICustomer, Model<ICustomer>, ICustomer>({
name: {