Skip to content

Commit a22b238

Browse files
Merge pull request #79 from mijinummi/feat/tenant-benefit-marketplace
Tenant Benefit Marketplace API (#38)
2 parents 392c5d1 + b194901 commit a22b238

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const marketplaceService = require('../services/marketplace.service');
2+
3+
class MarketplaceController {
4+
async listDeals(req, res) {
5+
try {
6+
const userId = req.user.id;
7+
const deals = await marketplaceService.listDeals(userId);
8+
res.json(deals);
9+
} catch (err) {
10+
res.status(403).json({ error: err.message });
11+
}
12+
}
13+
14+
async getDeal(req, res) {
15+
try {
16+
const userId = req.user.id;
17+
const dealId = req.params.id;
18+
const deal = await marketplaceService.getDeal(userId, dealId);
19+
res.json(deal);
20+
} catch (err) {
21+
res.status(403).json({ error: err.message });
22+
}
23+
}
24+
}
25+
26+
module.exports = new MarketplaceController();

src/routes/marketplace.routes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const express = require('express');
2+
const router = express.Router();
3+
const marketplaceController = require('../controllers/marketplace.controller');
4+
const { authMiddleware } = require('../services/auth.service');
5+
6+
router.get('/marketplace/deals', authMiddleware, (req, res) => marketplaceController.listDeals(req, res));
7+
router.get('/marketplace/deals/:id', authMiddleware, (req, res) => marketplaceController.getDeal(req, res));
8+
9+
module.exports = router;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const db = require('../db');
2+
3+
class MarketplaceService {
4+
async listDeals(userId) {
5+
// Fetch tenant score
6+
const tenant = await db('tenants').where({ id: userId }).first();
7+
if (!tenant) throw new Error('Tenant not found');
8+
9+
// Only high-score tenants qualify (e.g. score >= 700)
10+
const score = tenant.creditScore;
11+
const deals = await db('partner_deals').select('*');
12+
13+
return deals.filter(d => score >= d.minScore);
14+
}
15+
16+
async getDeal(userId, dealId) {
17+
const tenant = await db('tenants').where({ id: userId }).first();
18+
if (!tenant) throw new Error('Tenant not found');
19+
20+
const deal = await db('partner_deals').where({ id: dealId }).first();
21+
if (!deal) throw new Error('Deal not found');
22+
23+
if (tenant.creditScore < deal.minScore) {
24+
throw new Error('Unauthorized: tenant score too low');
25+
}
26+
27+
return deal;
28+
}
29+
}
30+
31+
module.exports = new MarketplaceService();

tests/marketplace.e2e-spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const request = require('supertest');
2+
const app = require('../index');
3+
4+
describe('Tenant Benefit Marketplace API', () => {
5+
it('GET /marketplace/deals returns deals for high-score tenant', async () => {
6+
const res = await request(app)
7+
.get('/marketplace/deals')
8+
.set('Authorization', 'Bearer highScoreTenantToken');
9+
expect(res.status).toBe(200);
10+
expect(Array.isArray(res.body)).toBe(true);
11+
});
12+
13+
it('GET /marketplace/deals/:id returns deal if tenant qualifies', async () => {
14+
const res = await request(app)
15+
.get('/marketplace/deals/deal-123')
16+
.set('Authorization', 'Bearer highScoreTenantToken');
17+
expect(res.status).toBe(200);
18+
expect(res.body).toHaveProperty('id', 'deal-123');
19+
});
20+
21+
it('GET /marketplace/deals/:id denies low-score tenant', async () => {
22+
const res = await request(app)
23+
.get('/marketplace/deals/deal-123')
24+
.set('Authorization', 'Bearer lowScoreTenantToken');
25+
expect(res.status).toBe(403);
26+
});
27+
});

0 commit comments

Comments
 (0)