Commit 933b118d by Augusto

small partners fixes

parent 1ae1b266
...@@ -11,6 +11,7 @@ FROM "Inspection" ...@@ -11,6 +11,7 @@ FROM "Inspection"
WHERE comment LIKE '%Auto-generated%' WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%' OR comment LIKE '%Generated inspection%'
OR comment LIKE '%pending completion%' OR comment LIKE '%pending completion%'
OR comment LIKE '%Automatically added for new question%'
ORDER BY "createdAt" DESC; ORDER BY "createdAt" DESC;
-- Count how many records will be affected -- Count how many records will be affected
...@@ -18,20 +19,23 @@ SELECT COUNT(*) as affected_inspections ...@@ -18,20 +19,23 @@ SELECT COUNT(*) as affected_inspections
FROM "Inspection" FROM "Inspection"
WHERE comment LIKE '%Auto-generated%' WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%' 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 -- Clean up inspection comments
UPDATE "Inspection" UPDATE "Inspection"
SET comment = NULL SET comment = NULL
WHERE comment LIKE '%Auto-generated%' WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%' 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 -- Clean up inspection response comments
UPDATE "InspectionResponse" UPDATE "InspectionResponse"
SET comment = NULL SET comment = NULL
WHERE comment LIKE '%Auto-generated%' 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 -- Show results after cleanup
SELECT SELECT
...@@ -41,6 +45,7 @@ FROM "Inspection" ...@@ -41,6 +45,7 @@ FROM "Inspection"
WHERE comment LIKE '%Auto-generated%' WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%' OR comment LIKE '%Generated inspection%'
OR comment LIKE '%pending completion%' OR comment LIKE '%pending completion%'
OR comment LIKE '%Automatically added for new question%'
UNION ALL UNION ALL
...@@ -49,7 +54,8 @@ SELECT ...@@ -49,7 +54,8 @@ SELECT
COUNT(*) as remaining_records COUNT(*) as remaining_records
FROM "InspectionResponse" FROM "InspectionResponse"
WHERE comment LIKE '%Auto-generated%' 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 -- Verify cleanup was successful
SELECT SELECT
...@@ -63,10 +69,12 @@ FROM ( ...@@ -63,10 +69,12 @@ FROM (
WHERE comment LIKE '%Auto-generated%' WHERE comment LIKE '%Auto-generated%'
OR comment LIKE '%Generated inspection%' OR comment LIKE '%Generated inspection%'
OR comment LIKE '%pending completion%' OR comment LIKE '%pending completion%'
OR comment LIKE '%Automatically added for new question%'
UNION ALL UNION ALL
SELECT id FROM "InspectionResponse" SELECT id FROM "InspectionResponse"
WHERE comment LIKE '%Auto-generated%' 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%'
) as remaining_comments; ) as remaining_comments;
...@@ -43,4 +43,13 @@ export class FindInspectionDto { ...@@ -43,4 +43,13 @@ export class FindInspectionDto {
@IsEnum(inspectionStatusEnum.enumValues) @IsEnum(inspectionStatusEnum.enumValues)
@IsOptional() @IsOptional()
status?: (typeof inspectionStatusEnum.enumValues)[number]; 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 { ...@@ -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() @Get()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ApiOperation({ @ApiOperation({
......
...@@ -24,6 +24,8 @@ import { ...@@ -24,6 +24,8 @@ import {
asc, asc,
desc, desc,
inArray, inArray,
gte,
lte,
SQL, SQL,
} from 'drizzle-orm'; } from 'drizzle-orm';
import { QuestionsService } from '../questions/questions.service'; import { QuestionsService } from '../questions/questions.service';
...@@ -239,6 +241,18 @@ export class InspectionService { ...@@ -239,6 +241,18 @@ export class InspectionService {
conditions.push(eq(inspections.status, dto.status as any)); 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 = const whereCondition =
conditions.length > 0 ? and(...conditions) : undefined; conditions.length > 0 ? and(...conditions) : undefined;
...@@ -789,6 +803,50 @@ export class InspectionService { ...@@ -789,6 +803,50 @@ export class InspectionService {
return this.mapToInspectionDto(updatedInspection); 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( private mapToInspectionDto(
inspection: any, inspection: any,
responses: any[] = [], responses: any[] = [],
...@@ -797,7 +855,7 @@ export class InspectionService { ...@@ -797,7 +855,7 @@ export class InspectionService {
return { return {
id: inspection.id, id: inspection.id,
date: inspection.date, date: inspection.date,
deadline: inspection.deadline, deadline: inspection.deadline || null, // Handle missing deadline field
comment: inspection.comment, comment: inspection.comment,
finalComment: inspection.finalComment, finalComment: inspection.finalComment,
finalCommentStatus: inspection.finalCommentStatus, finalCommentStatus: inspection.finalCommentStatus,
......
...@@ -16,10 +16,10 @@ export class PartnerUserDto { ...@@ -16,10 +16,10 @@ export class PartnerUserDto {
export class PartnerCountDto { export class PartnerCountDto {
@ApiProperty({ @ApiProperty({
description: 'Number of candidates associated with this partner', description: 'Number of sites associated with this partner',
example: 42, example: 5,
}) })
candidates: number; sites: number;
} }
export class PartnerResponseDto { export class PartnerResponseDto {
......
...@@ -48,10 +48,23 @@ export class PartnersService { ...@@ -48,10 +48,23 @@ export class PartnersService {
.where(eq(users.partnerId, partner.id)); .where(eq(users.partnerId, partner.id));
// Get site count for this partner // Get site count for this partner
const [{ siteCount }] = await this.databaseService.db let siteCount = 0;
.select({ siteCount: count(partnerSites.id) }) try {
.from(partnerSites) const result = await this.databaseService.db
.where(eq(partnerSites.partnerId, partner.id)); .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 { return {
...partner, ...partner,
...@@ -193,33 +206,44 @@ export class PartnersService { ...@@ -193,33 +206,44 @@ export class PartnersService {
async getPartnerSites(partnerId: number) { async getPartnerSites(partnerId: number) {
await this.findOne(partnerId); await this.findOne(partnerId);
// Get sites for this partner try {
const partnerSitesList = await this.databaseService.db // Get sites for this partner
.select() const partnerSitesList = await this.databaseService.db
.from(partnerSites) .select()
.where(eq(partnerSites.partnerId, partnerId)); .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 return sitesWithDetails;
const sitesWithDetails = await Promise.all( } catch (error) {
partnerSitesList.map(async (partnerSite) => { console.error('❌ [DEBUG] Error in getPartnerSites:', {
const siteList = await this.databaseService.db message: error.message,
.select() partnerId: partnerId,
.from(sites) stack: error.stack,
.where(eq(sites.id, partnerSite.siteId)) });
.limit(1);
return { // Return empty array if there's an error
partnerSiteId: partnerSite.id, return [];
partnerId: partnerSite.partnerId, }
siteId: partnerSite.siteId,
site: siteList.length > 0 ? siteList[0] : null,
createdAt: partnerSite.createdAt,
updatedAt: partnerSite.updatedAt,
};
}),
);
return sitesWithDetails;
} }
async addSiteToPartner(partnerId: number, siteId: number) { 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