Files
datacat/backend/controllers/userController.js
GandClaude Opus 5 53d2e70723 chore: add a formatter (#233)
* chore: add a formatter, matching the style this repo already writes

singleQuote=true was chosen by counting this repo's own
imports, not by fleet decree. The fleet is genuinely split and the two repos
that already had a .prettierrc disagreed with each other, so there was no
standard to restore. Quote style does not cross repo boundaries; having a
gate does.

Markdown is ignored for now — prettier rewraps prose, which would bury the
real diff.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* style: format with prettier (199 files)

Mechanical. No behaviour change. This SHA is listed in
.git-blame-ignore-revs so `git blame` skips it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore: teach git blame to skip the reformat

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(ci): put prettier where CI actually installs

The formatter was added to the root package.json, but CI installs with
working-directory: frontend and then runs npm run verify at the root, so root
devDependencies are never installed at all. format:check died with
'prettier: not found' before checking a single file.

Root now delegates into frontend the way every other root script already does,
and prettier is a devDependency of frontend, whose lockfile CI installs.

frontend also needs its own .prettierignore: prettier resolves that file
relative to the working directory, so the root one stopped applying the moment
the command ran from frontend/ — which is how README.md and Playwright output
ended up being checked.

* fix(ci): do not format contentlayer's generated output

format:check ran green locally and failed in CI on files that do not exist
locally: CI builds contentlayer before verify, so .contentlayer/generated is
present by the time prettier looks. One generated module imports with
assert { type: 'json' }, which prettier's parser rejects outright.

A local --check cannot see build output, so generated directories have to be
ignored by name rather than discovered.

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-29 11:06:25 +00:00

168 lines
3.7 KiB
JavaScript

const prisma = require('../lib/prisma');
const { success, error, notFound } = require('../utils/responseHandlers');
exports.getMe = async (req, res) => {
try {
// req.user is attached by the auth middleware and already contains user data
// But let's fetch fresh data from database
const user = await prisma.user.findUnique({
where: { id: req.user.id },
select: {
id: true,
email: true,
name: true,
role: true,
avatar: true,
emailVerified: true,
isActive: true,
createdAt: true,
updatedAt: true,
organization: {
select: {
id: true,
name: true,
slug: true,
},
},
_count: {
select: {
forms: true,
submissions: true,
},
},
},
});
if (!user) {
return notFound(res, 'User not found');
}
return success(res, { user });
} catch (err) {
console.error('Get user error:', err);
return error(res, 'Server error getting user data', 500, err);
}
};
exports.updateUser = async (req, res) => {
const { name, avatar } = req.body;
const userId = req.user.id;
try {
const user = await prisma.user.update({
where: { id: userId },
data: {
...(name && { name }),
...(avatar && { avatar }),
updatedAt: new Date(),
},
select: {
id: true,
email: true,
name: true,
avatar: true,
updatedAt: true,
},
});
return success(res, { user }, 'User updated successfully');
} catch (err) {
console.error('Update user error:', err);
return error(res, 'Server error updating user', 500, err);
}
};
exports.deleteUser = async (req, res) => {
const userId = req.user.id;
try {
// This is a sensitive operation - in production you might want to:
// 1. Send confirmation email
// 2. Soft delete instead of hard delete
// 3. Archive user data instead of deleting
await prisma.user.update({
where: { id: userId },
data: {
isActive: false,
updatedAt: new Date(),
},
});
return success(res, {}, 'User account deactivated successfully');
} catch (err) {
console.error('Delete user error:', err);
return error(res, 'Server error deleting user', 500, err);
}
};
exports.getUserStats = async (req, res) => {
const userId = req.user.id;
try {
const [formsCount, submissionsCount, publishedFormsCount] = await Promise.all([
prisma.form.count({
where: { userId },
}),
prisma.submission.count({
where: {
form: { userId },
},
}),
prisma.form.count({
where: {
userId,
isPublished: true,
},
}),
]);
// Get recent activity
const recentForms = await prisma.form.findMany({
where: { userId },
select: {
id: true,
title: true,
isPublished: true,
updatedAt: true,
},
orderBy: {
updatedAt: 'desc',
},
take: 5,
});
const recentSubmissions = await prisma.submission.findMany({
where: {
form: { userId },
},
select: {
id: true,
submittedAt: true,
form: {
select: {
title: true,
},
},
},
orderBy: {
submittedAt: 'desc',
},
take: 5,
});
return success(res, {
stats: {
formsCount,
submissionsCount,
publishedFormsCount,
recentForms,
recentSubmissions,
},
});
} catch (err) {
console.error('Get user stats error:', err);
return error(res, 'Server error getting user stats', 500, err);
}
};