103 lines
4.3 KiB
JavaScript
103 lines
4.3 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ProcessOrgAuthCallbackCommandHandler = void 0;
|
|
const Logger_1 = require("../../Services/Logger");
|
|
class ProcessOrgAuthCallbackCommandHandler {
|
|
constructor(userRepo, orgRepo) {
|
|
this.userRepo = userRepo;
|
|
this.orgRepo = orgRepo;
|
|
}
|
|
async execute(cmd) {
|
|
const startTime = Date.now();
|
|
try {
|
|
(0, Logger_1.logAuth)('Processing organization authentication callback', cmd.userId, {
|
|
organizationId: cmd.organizationId,
|
|
status: cmd.status,
|
|
hasAuthToken: !!cmd.authToken
|
|
});
|
|
// Verify organization exists
|
|
const organization = await this.orgRepo.findById(cmd.organizationId);
|
|
if (!organization) {
|
|
(0, Logger_1.logWarning)('Organization not found for auth callback', {
|
|
organizationId: cmd.organizationId,
|
|
userId: cmd.userId
|
|
});
|
|
return {
|
|
success: false,
|
|
message: 'Organization not found'
|
|
};
|
|
}
|
|
// Verify user exists
|
|
const user = await this.userRepo.findById(cmd.userId);
|
|
if (!user) {
|
|
(0, Logger_1.logWarning)('User not found for auth callback', {
|
|
organizationId: cmd.organizationId,
|
|
userId: cmd.userId
|
|
});
|
|
return {
|
|
success: false,
|
|
message: 'User not found'
|
|
};
|
|
}
|
|
// Verify user belongs to the organization
|
|
if (user.orgid !== cmd.organizationId) {
|
|
(0, Logger_1.logWarning)('User does not belong to organization for auth callback', {
|
|
organizationId: cmd.organizationId,
|
|
userId: cmd.userId,
|
|
userOrgId: user.orgid
|
|
});
|
|
return {
|
|
success: false,
|
|
message: 'User does not belong to this organization'
|
|
};
|
|
}
|
|
if (cmd.status === 'not_ok') {
|
|
(0, Logger_1.logAuth)('Organization authentication failed', cmd.userId, {
|
|
organizationId: cmd.organizationId,
|
|
organizationName: organization.name
|
|
});
|
|
return {
|
|
success: false,
|
|
message: 'Organization authentication failed'
|
|
};
|
|
}
|
|
// Update user's organization login date
|
|
const now = new Date();
|
|
const updatedUser = await this.userRepo.update(cmd.userId, {
|
|
Orglogindate: now
|
|
});
|
|
if (!updatedUser) {
|
|
(0, Logger_1.logError)('Failed to update user organization login date', new Error('User update returned null'));
|
|
return {
|
|
success: false,
|
|
message: 'Failed to update user login information'
|
|
};
|
|
}
|
|
(0, Logger_1.logAuth)('Organization authentication successful', cmd.userId, {
|
|
organizationId: cmd.organizationId,
|
|
organizationName: organization.name,
|
|
orgLoginDate: now.toISOString(),
|
|
executionTime: Date.now() - startTime
|
|
});
|
|
(0, Logger_1.logDatabase)('User organization login date updated', `userId: ${cmd.userId}, orgId: ${cmd.organizationId}`, Date.now() - startTime, {
|
|
userId: cmd.userId,
|
|
organizationId: cmd.organizationId,
|
|
newOrgLoginDate: now.toISOString()
|
|
});
|
|
return {
|
|
success: true,
|
|
message: 'Organization authentication successful',
|
|
updatedFields: ['Orglogindate']
|
|
};
|
|
}
|
|
catch (error) {
|
|
(0, Logger_1.logError)('ProcessOrgAuthCallbackCommandHandler error', error);
|
|
return {
|
|
success: false,
|
|
message: 'Internal error processing authentication callback'
|
|
};
|
|
}
|
|
}
|
|
}
|
|
exports.ProcessOrgAuthCallbackCommandHandler = ProcessOrgAuthCallbackCommandHandler;
|
|
//# sourceMappingURL=ProcessOrgAuthCallbackCommandHandler.js.map
|