Files
adopt-a-street/backend/models/Badge.js
William Valentin 5efee88655 feat: complete MongoDB to CouchDB migration and deployment
- Remove all mongoose dependencies from backend
- Convert Badge and PointTransaction models to CouchDB
- Fix gamificationService for CouchDB architecture
- Update Docker registry URLs to use HTTPS (port 443)
- Fix ingress configuration for HAProxy
- Successfully deploy multi-architecture images
- Application fully running on Kubernetes with CouchDB

🤖 Generated with [AI Assistant]

Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
2025-11-02 14:39:49 -08:00

127 lines
3.2 KiB
JavaScript

const couchdbService = require("../services/couchdbService");
class Badge {
static async findAll() {
try {
const result = await couchdbService.find({
selector: { type: 'badge' },
sort: [{ order: 'asc' }]
});
return result.docs;
} catch (error) {
console.error('Error finding badges:', error);
throw error;
}
}
static async findById(id) {
try {
const badge = await couchdbService.get(id);
if (badge.type !== 'badge') {
return null;
}
return badge;
} catch (error) {
if (error.statusCode === 404) {
return null;
}
console.error('Error finding badge by ID:', error);
throw error;
}
}
static async create(badgeData) {
try {
const badge = {
_id: `badge_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
type: 'badge',
name: badgeData.name,
description: badgeData.description,
icon: badgeData.icon,
criteria: badgeData.criteria,
rarity: badgeData.rarity || 'common',
order: badgeData.order || 0,
isActive: badgeData.isActive !== undefined ? badgeData.isActive : true,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
};
const result = await couchdbService.insert(badge);
return { ...badge, _rev: result.rev };
} catch (error) {
console.error('Error creating badge:', error);
throw error;
}
}
static async update(id, updateData) {
try {
const existingBadge = await couchdbService.get(id);
if (existingBadge.type !== 'badge') {
throw new Error('Document is not a badge');
}
const updatedBadge = {
...existingBadge,
...updateData,
updatedAt: new Date().toISOString()
};
const result = await couchdbService.insert(updatedBadge);
return { ...updatedBadge, _rev: result.rev };
} catch (error) {
console.error('Error updating badge:', error);
throw error;
}
}
static async delete(id) {
try {
const badge = await couchdbService.get(id);
if (badge.type !== 'badge') {
throw new Error('Document is not a badge');
}
await couchdbService.destroy(id, badge._rev);
return true;
} catch (error) {
console.error('Error deleting badge:', error);
throw error;
}
}
static async findByCriteria(criteriaType, threshold) {
try {
const result = await couchdbService.find({
selector: {
type: 'badge',
'criteria.type': criteriaType,
'criteria.threshold': { $lte: threshold }
},
sort: [{ 'criteria.threshold': 'desc' }]
});
return result.docs;
} catch (error) {
console.error('Error finding badges by criteria:', error);
throw error;
}
}
static async findByRarity(rarity) {
try {
const result = await couchdbService.find({
selector: {
type: 'badge',
rarity: rarity
},
sort: [{ order: 'asc' }]
});
return result.docs;
} catch (error) {
console.error('Error finding badges by rarity:', error);
throw error;
}
}
}
module.exports = Badge;