Skip to content

Commit b64c893

Browse files
committed
Fix sites sync - handle missing displayName
1 parent b949991 commit b64c893

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

src/trpc/routers/sites.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ export const sitesRouter = createTRPCRouter({
182182
let updated = 0;
183183

184184
for (const site of allSites) {
185+
// Extract display name with fallbacks
186+
const displayName = site.displayName || site.name || extractSiteNameFromUrl(site.webUrl) || 'Unnamed Site';
187+
185188
const existing = await db.sharePointSite.findUnique({
186189
where: { graphId: site.id },
187190
});
@@ -190,7 +193,7 @@ export const sitesRouter = createTRPCRouter({
190193
await db.sharePointSite.update({
191194
where: { graphId: site.id },
192195
data: {
193-
displayName: site.displayName || site.name,
196+
displayName,
194197
webUrl: site.webUrl,
195198
siteCollection: site.siteCollection?.hostname || null,
196199
updatedAt: new Date(),
@@ -201,7 +204,7 @@ export const sitesRouter = createTRPCRouter({
201204
await db.sharePointSite.create({
202205
data: {
203206
graphId: site.id,
204-
displayName: site.displayName || site.name,
207+
displayName,
205208
webUrl: site.webUrl,
206209
siteCollection: site.siteCollection?.hostname || null,
207210
},
@@ -225,3 +228,28 @@ export const sitesRouter = createTRPCRouter({
225228
}
226229
}),
227230
});
231+
232+
/**
233+
* Extract site name from URL as fallback when displayName is missing
234+
*/
235+
function extractSiteNameFromUrl(url: string): string | null {
236+
try {
237+
const urlObj = new URL(url);
238+
const pathParts = urlObj.pathname.split('/').filter(Boolean);
239+
240+
// Try to get the last meaningful part of the path
241+
// e.g., /sites/MySite -> MySite, /teams/MyTeam -> MyTeam
242+
if (pathParts.length >= 2) {
243+
return pathParts[pathParts.length - 1];
244+
}
245+
246+
// If just root, use hostname
247+
if (pathParts.length === 0 || (pathParts.length === 1 && pathParts[0] === 'search')) {
248+
return urlObj.hostname.split('.')[0];
249+
}
250+
251+
return pathParts[0] || null;
252+
} catch {
253+
return null;
254+
}
255+
}

0 commit comments

Comments
 (0)