Commit 933b118d by Augusto

small partners fixes

parent 1ae1b266
......@@ -11,6 +11,7 @@ FROM "Inspection"
WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%'
OR comment LIKE '%pending completion%'
OR comment LIKE '%Automatically added for new question%'
ORDER BY "createdAt" DESC;
-- Count how many records will be affected
......@@ -18,20 +19,23 @@ SELECT COUNT(*) as affected_inspections
FROM "Inspection"
WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%'
OR comment LIKE '%pending completion%';
OR comment LIKE '%pending completion%'
OR comment LIKE '%Automatically added for new question%';
-- Clean up inspection comments
UPDATE "Inspection"
SET comment = NULL
WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%'
OR comment LIKE '%pending completion%';
OR comment LIKE '%pending completion%'
OR comment LIKE '%Automatically added for new question%';
-- Clean up inspection response comments
UPDATE "InspectionResponse"
SET comment = NULL
WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%to be completed%';
OR comment LIKE '%to be completed%'
OR comment LIKE '%Automatically added for new question%';
-- Show results after cleanup
SELECT
......@@ -41,6 +45,7 @@ FROM "Inspection"
WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%'
OR comment LIKE '%pending completion%'
OR comment LIKE '%Automatically added for new question%'
UNION ALL
......@@ -49,7 +54,8 @@ SELECT
COUNT(*) as remaining_records
FROM "InspectionResponse"
WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%to be completed%';
OR comment LIKE '%to be completed%'
OR comment LIKE '%Automatically added for new question%';
-- Verify cleanup was successful
SELECT
......@@ -63,10 +69,12 @@ FROM (
WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%'
OR comment LIKE '%pending completion%'
OR comment LIKE '%Automatically added for new question%'
UNION ALL
SELECT id FROM "InspectionResponse"
WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%to be completed%'
OR comment LIKE '%Automatically added for new question%'
) as remaining_comments;
......@@ -43,4 +43,13 @@ export class FindInspectionDto {
@IsEnum(inspectionStatusEnum.enumValues)
@IsOptional()
status?: (typeof inspectionStatusEnum.enumValues)[number];
@ApiPropertyOptional({
description: 'Filter inspection records by deadline date',
example: '2025-12-31T23:59:59.000Z',
type: String,
})
@IsDateString()
@IsOptional()
deadline?: string;
}
......@@ -170,6 +170,17 @@ export class InspectionController {
);
}
@Get('debug/schema')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(Role.ADMIN, Role.SUPERADMIN)
@ApiOperation({
summary: 'Debug endpoint to check database schema',
description: 'Debug endpoint to verify database connection and schema',
})
async debugSchema() {
return this.inspectionService.debugSchema();
}
@Get()
@UseGuards(JwtAuthGuard)
@ApiOperation({
......
......@@ -24,6 +24,8 @@ import {
asc,
desc,
inArray,
gte,
lte,
SQL,
} from 'drizzle-orm';
import { QuestionsService } from '../questions/questions.service';
......@@ -239,6 +241,18 @@ export class InspectionService {
conditions.push(eq(inspections.status, dto.status as any));
}
if (dto.startDate) {
conditions.push(gte(inspections.date, new Date(dto.startDate)));
}
if (dto.endDate) {
conditions.push(lte(inspections.date, new Date(dto.endDate)));
}
if (dto.deadline) {
conditions.push(eq(inspections.deadline, new Date(dto.deadline)));
}
const whereCondition =
conditions.length > 0 ? and(...conditions) : undefined;
......@@ -789,6 +803,50 @@ export class InspectionService {
return this.mapToInspectionDto(updatedInspection);
}
async debugSchema() {
try {
// Test 1: Check if deadline column exists
const columnCheck = await this.databaseService.db.execute(`
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'Inspection' AND column_name = 'deadline'
`);
// Test 2: Try to select from Inspection table with deadline
let testQuery;
try {
testQuery = await this.databaseService.db.execute(`
SELECT id, date, deadline, comment, "siteId"
FROM "Inspection"
LIMIT 1
`);
} catch (error) {
testQuery = { error: error.message };
}
// Test 3: Check all columns in Inspection table
const allColumns = await this.databaseService.db.execute(`
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'Inspection'
ORDER BY ordinal_position
`);
return {
deadlineColumnExists: columnCheck.rows.length > 0,
deadlineColumnInfo: columnCheck.rows,
testQueryResult: testQuery.rows || testQuery.error,
allColumns: allColumns.rows,
timestamp: new Date().toISOString(),
};
} catch (error) {
return {
error: error.message,
timestamp: new Date().toISOString(),
};
}
}
private mapToInspectionDto(
inspection: any,
responses: any[] = [],
......@@ -797,7 +855,7 @@ export class InspectionService {
return {
id: inspection.id,
date: inspection.date,
deadline: inspection.deadline,
deadline: inspection.deadline || null, // Handle missing deadline field
comment: inspection.comment,
finalComment: inspection.finalComment,
finalCommentStatus: inspection.finalCommentStatus,
......
......@@ -16,10 +16,10 @@ export class PartnerUserDto {
export class PartnerCountDto {
@ApiProperty({
description: 'Number of candidates associated with this partner',
example: 42,
description: 'Number of sites associated with this partner',
example: 5,
})
candidates: number;
sites: number;
}
export class PartnerResponseDto {
......
......@@ -48,10 +48,23 @@ export class PartnersService {
.where(eq(users.partnerId, partner.id));
// Get site count for this partner
const [{ siteCount }] = await this.databaseService.db
.select({ siteCount: count(partnerSites.id) })
.from(partnerSites)
.where(eq(partnerSites.partnerId, partner.id));
let siteCount = 0;
try {
const result = await this.databaseService.db
.select({ siteCount: count(partnerSites.id) })
.from(partnerSites)
.where(eq(partnerSites.partnerId, partner.id));
siteCount = result[0]?.siteCount || 0;
} catch (error) {
console.error(
'❌ [DEBUG] Error getting site count for partner',
partner.id,
':',
error.message,
);
// Set siteCount to 0 and continue
siteCount = 0;
}
return {
...partner,
......@@ -193,33 +206,44 @@ export class PartnersService {
async getPartnerSites(partnerId: number) {
await this.findOne(partnerId);
// Get sites for this partner
const partnerSitesList = await this.databaseService.db
.select()
.from(partnerSites)
.where(eq(partnerSites.partnerId, partnerId));
try {
// Get sites for this partner
const partnerSitesList = await this.databaseService.db
.select()
.from(partnerSites)
.where(eq(partnerSites.partnerId, partnerId));
// For each partner site, get the site details
const sitesWithDetails = await Promise.all(
partnerSitesList.map(async (partnerSite) => {
const siteList = await this.databaseService.db
.select()
.from(sites)
.where(eq(sites.id, partnerSite.siteId))
.limit(1);
return {
partnerSiteId: partnerSite.id,
partnerId: partnerSite.partnerId,
siteId: partnerSite.siteId,
site: siteList.length > 0 ? siteList[0] : null,
createdAt: partnerSite.createdAt,
updatedAt: partnerSite.updatedAt,
};
}),
);
// For each partner site, get the site details
const sitesWithDetails = await Promise.all(
partnerSitesList.map(async (partnerSite) => {
const siteList = await this.databaseService.db
.select()
.from(sites)
.where(eq(sites.id, partnerSite.siteId))
.limit(1);
return sitesWithDetails;
} catch (error) {
console.error('❌ [DEBUG] Error in getPartnerSites:', {
message: error.message,
partnerId: partnerId,
stack: error.stack,
});
return {
partnerSiteId: partnerSite.id,
partnerId: partnerSite.partnerId,
siteId: partnerSite.siteId,
site: siteList.length > 0 ? siteList[0] : null,
createdAt: partnerSite.createdAt,
updatedAt: partnerSite.updatedAt,
};
}),
);
return sitesWithDetails;
// Return empty array if there's an error
return [];
}
}
async addSiteToPartner(partnerId: number, siteId: number) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment