notification about emails in company card
This commit is contained in:
@@ -69,6 +69,25 @@ export const getCompanyEmailThreads = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get unread email counts grouped by company for current user
|
||||||
|
* GET /api/companies/email-unread
|
||||||
|
*/
|
||||||
|
export const getCompanyUnreadCounts = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const userId = req.userId;
|
||||||
|
const counts = await companyEmailService.getCompanyUnreadCounts(userId);
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
data: counts,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
|
||||||
|
res.status(error.statusCode || 500).json(errorResponse);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get company with relations (projects, todos, notes)
|
* Get company with relations (projects, todos, notes)
|
||||||
* GET /api/companies/:companyId/details
|
* GET /api/companies/:companyId/details
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ router.use(authenticate);
|
|||||||
router.get('/reminders/summary', companyController.getReminderSummary);
|
router.get('/reminders/summary', companyController.getReminderSummary);
|
||||||
router.get('/reminders/counts', companyController.getReminderCountsByCompany);
|
router.get('/reminders/counts', companyController.getReminderCountsByCompany);
|
||||||
|
|
||||||
|
// Company unread email summary
|
||||||
|
router.get('/email-unread', companyController.getCompanyUnreadCounts);
|
||||||
|
|
||||||
// Company email threads
|
// Company email threads
|
||||||
router.get(
|
router.get(
|
||||||
'/:companyId/email-threads',
|
'/:companyId/email-threads',
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { and, desc, eq, inArray } from 'drizzle-orm';
|
import { and, desc, eq, inArray, isNull, not, sql } from 'drizzle-orm';
|
||||||
import { db } from '../config/database.js';
|
import { db } from '../config/database.js';
|
||||||
import { contacts, emailAccounts, emails, userEmailAccounts } from '../db/schema.js';
|
import { contacts, emailAccounts, emails, userEmailAccounts } from '../db/schema.js';
|
||||||
|
|
||||||
@@ -98,3 +98,39 @@ export const getCompanyEmailThreads = async (companyId, userId) => {
|
|||||||
threads,
|
threads,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getCompanyUnreadCounts = async (userId) => {
|
||||||
|
const accountLinks = await db
|
||||||
|
.select({ id: emailAccounts.id })
|
||||||
|
.from(userEmailAccounts)
|
||||||
|
.innerJoin(emailAccounts, eq(userEmailAccounts.emailAccountId, emailAccounts.id))
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(userEmailAccounts.userId, userId),
|
||||||
|
eq(emailAccounts.isActive, true)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
const accountIds = accountLinks.map((acc) => acc.id);
|
||||||
|
|
||||||
|
if (accountIds.length === 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const rows = await db
|
||||||
|
.select({
|
||||||
|
companyId: emails.companyId,
|
||||||
|
unreadCount: sql`count(*)::int`,
|
||||||
|
})
|
||||||
|
.from(emails)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
inArray(emails.emailAccountId, accountIds),
|
||||||
|
eq(emails.isRead, false),
|
||||||
|
not(isNull(emails.companyId))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.groupBy(emails.companyId);
|
||||||
|
|
||||||
|
return rows;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user