d7b374f8ab
New features: - ActivityController and manager for CW sales activities (CRUD) - ForecastProductController for opportunity forecast/product lines - CW member cache with dual-layer (in-memory + Redis) resolution - Catalog category/subcategory/ecosystem taxonomy module - Quote statuses type definitions with CW mapping - User-defined fields (UDF) module with cache and event refresh - Company sites CW module with serialization - Procurement manager filters (category, ecosystem, manufacturer, price, stock) - Opportunity notes CRUD and product line management via CW API - Opportunity type definitions endpoint Updates: - OpportunityController: CW refresh, company hydration, activities, custom fields - UserController: cwIdentifier field for CW member linking - CatalogItemController: category/subcategory fields from CW - PermissionNodes: sales note/product CRUD nodes, subCategories, collectPermissions - API routes: procurement categories/filters, sales notes/products, opportunity types - Global events: UDF and member refresh intervals on startup Tests (414 passing): - ActivityController, ForecastProductController, OpportunityController unit tests - UserController cwIdentifier tests - catalogCategories, companySites, memberCache, procurement module tests - activityTypes, opportunityTypes, quoteStatuses type tests - permissionNodes subCategories and getAllPermissionNodes tests - Updated test setup with redis mock, API method mocks, and builder helpers
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import { connectWiseApi, prisma } from "../../../constants";
|
|
import { events } from "../../globalEvents";
|
|
import { fetchAllCwMembers, findCwIdentifierByEmail } from "./fetchAllMembers";
|
|
import { setMemberCache } from "./memberCache";
|
|
|
|
/**
|
|
* Refresh CW Identifiers
|
|
*
|
|
* Fetches all CW members and all users from the database, then updates
|
|
* each user's `cwIdentifier` field by matching their email to a CW member's
|
|
* `officeEmail`. Only users whose identifier has changed (or was previously
|
|
* null) are updated to avoid unnecessary writes.
|
|
*
|
|
* Also refreshes the in-memory member cache used for name resolution.
|
|
*/
|
|
export const refreshCwIdentifiers = async () => {
|
|
events.emit("cw:members:refresh:started");
|
|
|
|
const allMembers = await fetchAllCwMembers();
|
|
await setMemberCache(allMembers);
|
|
const allUsers = await prisma.user.findMany({
|
|
select: { id: true, email: true, cwIdentifier: true },
|
|
});
|
|
|
|
let updatedCount = 0;
|
|
|
|
await Promise.all(
|
|
allUsers.map(async (user) => {
|
|
const identifier = await findCwIdentifierByEmail(user.email, allMembers);
|
|
|
|
if (identifier !== user.cwIdentifier) {
|
|
await prisma.user.update({
|
|
where: { id: user.id },
|
|
data: { cwIdentifier: identifier },
|
|
});
|
|
updatedCount++;
|
|
}
|
|
}),
|
|
);
|
|
|
|
events.emit("cw:members:refresh:completed", {
|
|
totalMembers: allMembers.size,
|
|
totalUsers: allUsers.length,
|
|
usersUpdated: updatedCount,
|
|
});
|
|
};
|