domain-reg / src /routes /accounts.js
Hdhdjdjd dhjdbd hjdhd
Deploy Zone.ID Domain API 2025-12-07
3d8743c
const { PrismaClient } = require('@prisma/client');
const { refreshAccountToken, refreshAllAccountTokens } = require('../../lib/token-scheduler');
const prisma = new PrismaClient();
async function accountRoutes(fastify, options) {
fastify.get('/', {
schema: {
summary: 'List all accounts',
tags: ['Accounts'],
security: [{ apiKey: [] }],
response: {
200: {
type: 'object',
properties: {
accounts: {
type: 'array',
items: {
type: 'object',
properties: {
id: { type: 'string' },
email: { type: 'string' },
name: { type: 'string' },
status: { type: 'string' },
domainCount: { type: 'integer' },
createdAt: { type: 'string' }
}
}
}
}
}
}
}
}, async (request, reply) => {
const accounts = await prisma.account.findMany({
select: {
id: true,
email: true,
name: true,
status: true,
domainCount: true,
createdAt: true
}
});
return { accounts };
});
fastify.get('/:id', {
schema: {
summary: 'Get account details',
tags: ['Accounts'],
security: [{ apiKey: [] }],
params: {
type: 'object',
properties: {
id: { type: 'string' }
}
},
response: {
200: {
type: 'object',
properties: {
id: { type: 'string' },
email: { type: 'string' },
name: { type: 'string' },
status: { type: 'string' },
domainCount: { type: 'integer' },
createdAt: { type: 'string' },
domains: {
type: 'array',
items: {
type: 'object',
properties: {
id: { type: 'string' },
subdomain: { type: 'string' },
suffix: { type: 'string' },
status: { type: 'string' }
}
}
}
}
}
}
}
}, async (request, reply) => {
const { id } = request.params;
const account = await prisma.account.findUnique({
where: { id },
include: {
domains: {
select: {
id: true,
subdomain: true,
suffix: true,
status: true
}
}
}
});
if (!account) {
return reply.code(404).send({ error: 'Account not found' });
}
return {
id: account.id,
email: account.email,
name: account.name,
status: account.status,
domainCount: account.domainCount,
createdAt: account.createdAt,
domains: account.domains
};
});
fastify.post('/:id/refresh-token', {
schema: {
summary: 'Refresh account token',
description: 'Manually refresh the Zone.ID access token for a specific account',
tags: ['Accounts'],
security: [{ apiKey: [] }],
params: {
type: 'object',
properties: {
id: { type: 'string' }
}
},
response: {
200: {
type: 'object',
properties: {
success: { type: 'boolean' },
message: { type: 'string' },
accountId: { type: 'string' }
}
}
}
}
}, async (request, reply) => {
const { id } = request.params;
const account = await prisma.account.findUnique({ where: { id } });
if (!account) {
return reply.code(404).send({ error: 'Account not found' });
}
if (!account.refreshToken) {
return reply.code(400).send({ error: 'Account has no refresh token' });
}
const result = await refreshAccountToken(account);
if (result.success) {
return {
success: true,
message: 'Token refreshed successfully',
accountId: account.id
};
} else {
return reply.code(400).send({
success: false,
error: 'Failed to refresh token',
reason: result.reason
});
}
});
fastify.post('/refresh-all-tokens', {
schema: {
summary: 'Refresh all account tokens',
description: 'Manually trigger token refresh for all active accounts',
tags: ['Accounts'],
security: [{ apiKey: [] }],
response: {
200: {
type: 'object',
properties: {
total: { type: 'integer' },
success: { type: 'integer' },
failed: { type: 'integer' },
skipped: { type: 'integer' }
}
}
}
}
}, async (request, reply) => {
const results = await refreshAllAccountTokens();
return {
total: results.total,
success: results.success,
failed: results.failed,
skipped: results.skipped,
details: results.details
};
});
}
module.exports = accountRoutes;