Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions platforms/eCurrency-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "ecurrency-api",
"version": "1.0.0",
"description": "eCurrency Platform API",
"main": "src/index.ts",
"scripts": {
"start": "ts-node --project tsconfig.json src/index.ts",
"dev": "nodemon --exec \"npx ts-node\" src/index.ts",
"build": "tsc",
"typeorm": "typeorm-ts-node-commonjs",
"migration:generate": "typeorm-ts-node-commonjs migration:generate -d src/database/data-source.ts",
"migration:run": "typeorm-ts-node-commonjs migration:run -d src/database/data-source.ts",
"migration:revert": "typeorm-ts-node-commonjs migration:revert -d src/database/data-source.ts"
},
"dependencies": {
"axios": "^1.6.7",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2",
"pg": "^8.11.3",
"reflect-metadata": "^0.2.1",
"typeorm": "^0.3.24",
"uuid": "^9.0.1",
"web3-adapter": "link:../../infrastructure/web3-adapter"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.5",
"@types/node": "^20.11.24",
"@types/pg": "^8.11.2",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"eslint": "^8.56.0",
"nodemon": "^3.0.3",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}

106 changes: 106 additions & 0 deletions platforms/eCurrency-api/src/controllers/AuthController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Request, Response } from "express";
import { v4 as uuidv4 } from "uuid";
import { UserService } from "../services/UserService";
import { EventEmitter } from "events";
import { signToken } from "../utils/jwt";

export class AuthController {
private userService: UserService;
private eventEmitter: EventEmitter;

constructor() {
this.userService = new UserService();
this.eventEmitter = new EventEmitter();
}

sseStream = async (req: Request, res: Response) => {
const { id } = req.params;

res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
"Access-Control-Allow-Origin": "*",
});

const handler = (data: any) => {
res.write(`data: ${JSON.stringify(data)}\n\n`);
};

this.eventEmitter.on(id, handler);

req.on("close", () => {
this.eventEmitter.off(id, handler);
res.end();
});

req.on("error", (error) => {
console.error("SSE Error:", error);
this.eventEmitter.off(id, handler);
res.end();
});
};

getOffer = async (req: Request, res: Response) => {
const baseUrl = process.env.VITE_ECURRENCY_BASE_URL || "http://localhost:9888";
const url = new URL(
"/api/auth",
baseUrl,
).toString();
const sessionId = uuidv4();
const offer = `w3ds://auth?redirect=${url}&session=${sessionId}&platform=ecurrency`;
res.json({ offer, sessionId });
};

login = async (req: Request, res: Response) => {
try {
const { ename, session, w3id, signature } = req.body;

if (!ename) {
return res.status(400).json({ error: "ename is required" });
}

if (!session) {
return res.status(400).json({ error: "session is required" });
}

// Only find existing users - don't create new ones during auth
const user = await this.userService.findUser(ename);

if (!user) {
// User doesn't exist - they need to be created via webhook first
return res.status(404).json({
error: "User not found",
message: "User must be created via eVault webhook before authentication"
});
}

const token = signToken({ userId: user.id });

const data = {
user: {
id: user.id,
ename: user.ename,
name: user.name,
handle: user.handle,
description: user.description,
avatarUrl: user.avatarUrl,
bannerUrl: user.bannerUrl,
isVerified: user.isVerified,
isPrivate: user.isPrivate,
email: user.email,
emailVerified: user.emailVerified,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
},
token,
};
this.eventEmitter.emit(session, data);
res.status(200).send();
} catch (error) {
console.error("Error during login:", error);
res.status(500).json({ error: "Internal server error" });
}
};
}

148 changes: 148 additions & 0 deletions platforms/eCurrency-api/src/controllers/CurrencyController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { Request, Response } from "express";
import { CurrencyService } from "../services/CurrencyService";
import { AccountType } from "../database/entities/Ledger";

export class CurrencyController {
private currencyService: CurrencyService;

constructor() {
this.currencyService = new CurrencyService();
}

createCurrency = async (req: Request, res: Response) => {
try {
if (!req.user) {
return res.status(401).json({ error: "Authentication required" });
}

const { name, description, groupId, allowNegative } = req.body;

if (!name || !groupId) {
return res.status(400).json({ error: "Name and groupId are required" });
}

const currency = await this.currencyService.createCurrency(
name,
groupId,
req.user.id,
allowNegative || false,
description
);

res.status(201).json({
id: currency.id,
name: currency.name,
description: currency.description,
ename: currency.ename,
groupId: currency.groupId,
allowNegative: currency.allowNegative,
createdBy: currency.createdBy,
createdAt: currency.createdAt,
updatedAt: currency.updatedAt,
});
} catch (error: any) {
console.error("Error creating currency:", error);
if (error.message.includes("Only group admins")) {
return res.status(403).json({ error: error.message });
}
res.status(500).json({ error: "Internal server error" });
}
};

getAllCurrencies = async (req: Request, res: Response) => {
try {
const currencies = await this.currencyService.getAllCurrencies();
res.json(currencies.map(currency => ({
id: currency.id,
name: currency.name,
ename: currency.ename,
groupId: currency.groupId,
allowNegative: currency.allowNegative,
createdBy: currency.createdBy,
createdAt: currency.createdAt,
updatedAt: currency.updatedAt,
})));
} catch (error) {
console.error("Error getting currencies:", error);
res.status(500).json({ error: "Internal server error" });
}
};

getCurrencyById = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const currency = await this.currencyService.getCurrencyById(id);

if (!currency) {
return res.status(404).json({ error: "Currency not found" });
}

res.json({
id: currency.id,
name: currency.name,
description: currency.description,
ename: currency.ename,
groupId: currency.groupId,
allowNegative: currency.allowNegative,
createdBy: currency.createdBy,
createdAt: currency.createdAt,
updatedAt: currency.updatedAt,
});
} catch (error) {
console.error("Error getting currency:", error);
res.status(500).json({ error: "Internal server error" });
}
};

getCurrenciesByGroup = async (req: Request, res: Response) => {
try {
const { groupId } = req.params;
const currencies = await this.currencyService.getCurrenciesByGroup(groupId);
res.json(currencies.map(currency => ({
id: currency.id,
name: currency.name,
description: currency.description,
ename: currency.ename,
groupId: currency.groupId,
allowNegative: currency.allowNegative,
createdBy: currency.createdBy,
createdAt: currency.createdAt,
updatedAt: currency.updatedAt,
})));
} catch (error) {
console.error("Error getting currencies by group:", error);
res.status(500).json({ error: "Internal server error" });
}
};

mintCurrency = async (req: Request, res: Response) => {
try {
if (!req.user) {
return res.status(401).json({ error: "Authentication required" });
}

const { id } = req.params;
const { amount, description } = req.body;

if (!amount) {
return res.status(400).json({ error: "amount is required" });
}

await this.currencyService.mintCurrency(
id,
amount,
description,
req.user.id
);

res.status(200).json({ message: "Currency minted successfully" });
} catch (error: any) {
console.error("Error minting currency:", error);
if (error.message.includes("Only group admins") || error.message.includes("not found") || error.message.includes("must be positive")) {
return res.status(400).json({ error: error.message });
}
res.status(500).json({ error: "Internal server error" });
}
};
}

75 changes: 75 additions & 0 deletions platforms/eCurrency-api/src/controllers/GroupController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Request, Response } from "express";
import { GroupService } from "../services/GroupService";

export class GroupController {
private groupService: GroupService;

constructor() {
this.groupService = new GroupService();
}

search = async (req: Request, res: Response) => {
try {
const { q, limit } = req.query;

if (!q || typeof q !== "string") {
return res.status(400).json({ error: "Query parameter 'q' is required" });
}

let limitNum = 10;
if (typeof limit === "string") {
const parsed = parseInt(limit, 10);
if (!Number.isNaN(parsed) && parsed > 0 && parsed <= 100) {
limitNum = parsed;
}
}

const groups = await this.groupService.searchGroups(q, limitNum);

res.json(groups.map(group => ({
id: group.id,
name: group.name,
ename: group.ename,
description: group.description,
charter: group.charter,
createdAt: group.createdAt,
updatedAt: group.updatedAt,
})));
} catch (error) {
console.error("Error searching groups:", error);
res.status(500).json({ error: "Internal server error" });
}
};

getUserGroups = async (req: Request, res: Response) => {
try {
if (!req.user) {
return res.status(401).json({ error: "Authentication required" });
}

const groups = await this.groupService.getUserGroups(req.user.id);

// Check which groups the user is admin of
const groupsWithAdminStatus = await Promise.all(
groups.map(async (group) => {
const isAdmin = await this.groupService.isGroupAdmin(group.id, req.user!.id);
return {
id: group.id,
name: group.name,
ename: group.ename,
description: group.description,
isAdmin,
createdAt: group.createdAt,
updatedAt: group.updatedAt,
};
})
);

res.json(groupsWithAdminStatus);
} catch (error) {
console.error("Error getting user groups:", error);
res.status(500).json({ error: "Internal server error" });
}
};
}

Loading
Loading