Added try catch and logs for connexion
This commit is contained in:
parent
4fd4dc82a8
commit
edcdb1a15c
@ -32,19 +32,24 @@ export default class UserController extends ApiController {
|
|||||||
protected async getUserInfosFromIdnot(req: Request, response: Response) {
|
protected async getUserInfosFromIdnot(req: Request, response: Response) {
|
||||||
try {
|
try {
|
||||||
const code = req.params["code"];
|
const code = req.params["code"];
|
||||||
|
console.log("code", code);
|
||||||
|
|
||||||
if (!code) throw new Error("code is required");
|
if (!code) throw new Error("code is required");
|
||||||
|
|
||||||
const idNotToken = await this.idNotService.getIdNotToken(code);
|
const idNotToken = await this.idNotService.getIdNotToken(code);
|
||||||
|
console.log("idNotToken", idNotToken);
|
||||||
|
|
||||||
if (!idNotToken) {
|
if (!idNotToken) {
|
||||||
|
console.error("IdNot token undefined");
|
||||||
this.httpValidationError(response, "IdNot token undefined");
|
this.httpValidationError(response, "IdNot token undefined");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await this.idNotService.getOrCreateUser(idNotToken);
|
const user = await this.idNotService.getOrCreateUser(idNotToken);
|
||||||
|
console.log("user", user);
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
console.error("User not found");
|
||||||
this.httpUnauthorized(response, "User not found");
|
this.httpUnauthorized(response, "User not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -56,6 +61,7 @@ export default class UserController extends ApiController {
|
|||||||
const prismaUser = await this.userService.getByUid(user.uid, { contact: true, role: true, office_membership: true });
|
const prismaUser = await this.userService.getByUid(user.uid, { contact: true, role: true, office_membership: true });
|
||||||
|
|
||||||
if (!prismaUser) {
|
if (!prismaUser) {
|
||||||
|
console.error("Prisma user not found");
|
||||||
this.httpNotFoundRequest(response, "user not found");
|
this.httpNotFoundRequest(response, "user not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -65,12 +71,12 @@ export default class UserController extends ApiController {
|
|||||||
|
|
||||||
|
|
||||||
if (!userHydrated.contact?.email || userHydrated.contact?.email === "") {
|
if (!userHydrated.contact?.email || userHydrated.contact?.email === "") {
|
||||||
|
console.error("Email not found");
|
||||||
this.httpUnauthorized(response, "Email not found");
|
this.httpUnauthorized(response, "Email not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let isSubscribed = await this.subscriptionsService.isUserSubscribed(user.uid, userHydrated.office_membership?.uid!);
|
let isSubscribed = await this.subscriptionsService.isUserSubscribed(user.uid, userHydrated.office_membership?.uid!);
|
||||||
|
|
||||||
|
|
||||||
//Check if user is whitelisted
|
//Check if user is whitelisted
|
||||||
// const isWhitelisted = await this.whitelistService.getByEmail(userHydrated.contact!.email);
|
// const isWhitelisted = await this.whitelistService.getByEmail(userHydrated.contact!.email);
|
||||||
|
|
||||||
@ -86,8 +92,13 @@ export default class UserController extends ApiController {
|
|||||||
await this.idNotService.updateOffice(user.office_uid);
|
await this.idNotService.updateOffice(user.office_uid);
|
||||||
|
|
||||||
const payload = await this.authService.getUserJwtPayload(user.idNot);
|
const payload = await this.authService.getUserJwtPayload(user.idNot);
|
||||||
|
console.log("payload", payload);
|
||||||
|
|
||||||
if (!payload) return;
|
|
||||||
|
if (!payload) {
|
||||||
|
console.error("No payload");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!isSubscribed && (userHydrated.role?.name === "admin" || userHydrated.role?.name === "super-admin")) {
|
if (!isSubscribed && (userHydrated.role?.name === "admin" || userHydrated.role?.name === "super-admin")) {
|
||||||
|
|
||||||
@ -107,6 +118,7 @@ export default class UserController extends ApiController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isSubscribed) {
|
if (!isSubscribed) {
|
||||||
|
console.error("User not subscribed");
|
||||||
this.httpUnauthorized(response, "User not subscribed");
|
this.httpUnauthorized(response, "User not subscribed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -122,15 +122,16 @@ export default class IdNotService extends BaseService {
|
|||||||
grant_type: "authorization_code",
|
grant_type: "authorization_code",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
const token = await fetch(this.variables.IDNOT_BASE_URL + this.variables.IDNOT_CONNEXION_URL + "?" + query, { method: "POST" });
|
const token = await fetch(this.variables.IDNOT_BASE_URL + this.variables.IDNOT_CONNEXION_URL + "?" + query, { method: "POST" });
|
||||||
|
|
||||||
if (token.status !== 200) console.error(await token.text());
|
if (token.status !== 200) console.error(await token.text());
|
||||||
|
|
||||||
const decodedToken = (await token.json()) as IIdNotToken;
|
const decodedToken = (await token.json()) as IIdNotToken;
|
||||||
|
|
||||||
const decodedIdToken = jwt.decode(decodedToken.id_token) as IdNotJwtPayload;
|
const decodedIdToken = jwt.decode(decodedToken.id_token) as IdNotJwtPayload;
|
||||||
|
|
||||||
return decodedIdToken;
|
return decodedIdToken;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getRole(roleName: string): Promise<Role> {
|
public async getRole(roleName: string): Promise<Role> {
|
||||||
@ -211,7 +212,10 @@ export default class IdNotService extends BaseService {
|
|||||||
const searchParams = new URLSearchParams({
|
const searchParams = new URLSearchParams({
|
||||||
key: this.variables.IDNOT_API_KEY,
|
key: this.variables.IDNOT_API_KEY,
|
||||||
});
|
});
|
||||||
let userData = (await (
|
|
||||||
|
let userData: IRattachementData;
|
||||||
|
try {
|
||||||
|
userData = (await (
|
||||||
await fetch(
|
await fetch(
|
||||||
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` +
|
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${user.idNot}_${user.office_membership!.idNot}?` +
|
||||||
searchParams,
|
searchParams,
|
||||||
@ -220,13 +224,25 @@ export default class IdNotService extends BaseService {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
).json()) as IRattachementData;
|
).json()) as IRattachementData;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching user data", error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!userData.statutDuRattachement) {
|
if (!userData.statutDuRattachement) {
|
||||||
const rattachements = (await (
|
let rattachements: any;
|
||||||
|
|
||||||
|
try {
|
||||||
|
rattachements = (await (
|
||||||
await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + searchParams, {
|
await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/personnes/${user.idNot}/rattachements?` + searchParams, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
})
|
})
|
||||||
).json()) as any;
|
).json()) as any;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching rattachements", error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (rattachements.totalResultCount === 0) {
|
if (rattachements.totalResultCount === 0) {
|
||||||
await this.userService.updateCheckedAt(user.uid!);
|
await this.userService.updateCheckedAt(user.uid!);
|
||||||
//await this.userService.delete(user.uid!);
|
//await this.userService.delete(user.uid!);
|
||||||
@ -236,11 +252,19 @@ export default class IdNotService extends BaseService {
|
|||||||
if (!rattachementsResults) return;
|
if (!rattachementsResults) return;
|
||||||
rattachementsResults.forEach(async (rattachement) => {
|
rattachementsResults.forEach(async (rattachement) => {
|
||||||
if (rattachement.statutDuRattachement) {
|
if (rattachement.statutDuRattachement) {
|
||||||
const officeData = (await (
|
let officeData: IOfficeData;
|
||||||
|
|
||||||
|
try {
|
||||||
|
officeData = (await (
|
||||||
await fetch(`${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` + searchParams, {
|
await fetch(`${this.variables.IDNOT_API_BASE_URL + rattachement.entiteUrl}?` + searchParams, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
})
|
})
|
||||||
).json()) as IOfficeData;
|
).json()) as IOfficeData;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching office data", error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (officeData.typeEntite.name === "office") {
|
if (officeData.typeEntite.name === "office") {
|
||||||
userData = rattachement;
|
userData = rattachement;
|
||||||
}
|
}
|
||||||
@ -254,9 +278,19 @@ export default class IdNotService extends BaseService {
|
|||||||
updates++;
|
updates++;
|
||||||
let officeData = (await this.officeService.get({ where: { idNot: userData.entite.ou } }))[0];
|
let officeData = (await this.officeService.get({ where: { idNot: userData.entite.ou } }))[0];
|
||||||
if (!officeData) {
|
if (!officeData) {
|
||||||
const officeLocationData = (await (
|
let officeLocationData: IOfficeLocation;
|
||||||
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" })
|
|
||||||
|
try {
|
||||||
|
officeLocationData = (await (
|
||||||
|
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, {
|
||||||
|
method: "GET",
|
||||||
|
})
|
||||||
).json()) as IOfficeLocation;
|
).json()) as IOfficeLocation;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching office location data", error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const office = {
|
const office = {
|
||||||
idNot: userData.entite.ou,
|
idNot: userData.entite.ou,
|
||||||
name: userData.entite.denominationSociale,
|
name: userData.entite.denominationSociale,
|
||||||
@ -290,21 +324,33 @@ export default class IdNotService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async updateOffice(officeId: string) {
|
public async updateOffice(officeId: string) {
|
||||||
const officeInfos = await this.officeService.getByUid(officeId);
|
const officeInfos = await this.officeService.getByUid(officeId, { address: true });
|
||||||
const office = Office.hydrate<Office>(officeInfos!);
|
const office = Office.hydrate<Office>(officeInfos!);
|
||||||
const searchParams = new URLSearchParams({
|
const searchParams = new URLSearchParams({
|
||||||
key: this.variables.IDNOT_API_KEY,
|
key: this.variables.IDNOT_API_KEY,
|
||||||
});
|
});
|
||||||
|
|
||||||
const officeRawData = await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/entites/${office.idNot}?` + searchParams, {
|
let officeRawData;
|
||||||
|
try {
|
||||||
|
officeRawData = await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/entites/${office.idNot}?` + searchParams, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching office data", error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (officeRawData.status === 404) {
|
if (officeRawData.status === 404) {
|
||||||
|
console.error("Fetching office raw data failed with status 404");
|
||||||
await this.officeService.updateCheckedAt(office.uid!);
|
await this.officeService.updateCheckedAt(office.uid!);
|
||||||
//await this.officeService.delete(office.uid!);
|
//await this.officeService.delete(office.uid!);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const officeData = (await officeRawData.json()) as IOfficeData;
|
const officeData = (await officeRawData.json()) as IOfficeData;
|
||||||
|
console.log("office", office);
|
||||||
|
|
||||||
|
console.log("officeData", officeData);
|
||||||
|
|
||||||
let updates = 0;
|
let updates = 0;
|
||||||
if (office.name !== officeData.denominationSociale) {
|
if (office.name !== officeData.denominationSociale) {
|
||||||
updates++;
|
updates++;
|
||||||
@ -325,19 +371,38 @@ export default class IdNotService extends BaseService {
|
|||||||
key: this.variables.IDNOT_API_KEY,
|
key: this.variables.IDNOT_API_KEY,
|
||||||
});
|
});
|
||||||
|
|
||||||
const userData = (await (
|
let userData: IRattachementData;
|
||||||
await fetch(`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${decodedToken.profile_idn}?` + searchParams, {
|
|
||||||
method: "GET",
|
|
||||||
})
|
|
||||||
).json()) as IRattachementData;
|
|
||||||
|
|
||||||
if (!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") {
|
try {
|
||||||
|
userData = (await (
|
||||||
|
await fetch(
|
||||||
|
`${this.variables.IDNOT_API_BASE_URL}/api/pp/v2/rattachements/${decodedToken.profile_idn}?` + searchParams,
|
||||||
|
{
|
||||||
|
method: "GET",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
).json()) as IRattachementData;
|
||||||
|
console.log("userData", userData);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const officeLocationData = (await (
|
if (!userData.statutDuRattachement || userData.entite.typeEntite.name !== "office") {
|
||||||
|
console.info("User not attached to an office (May be a partner)");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let officeLocationData: IOfficeLocation;
|
||||||
|
|
||||||
|
try {
|
||||||
|
officeLocationData = (await (
|
||||||
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" })
|
await fetch(`${this.variables.IDNOT_API_BASE_URL + userData.entite.locationsUrl}?` + searchParams, { method: "GET" })
|
||||||
).json()) as IOfficeLocation;
|
).json()) as IOfficeLocation;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const office = await this.officeService.get({ where: { idNot: decodedToken.entity_idn } });
|
const office = await this.officeService.get({ where: { idNot: decodedToken.entity_idn } });
|
||||||
|
|
||||||
@ -378,6 +443,7 @@ export default class IdNotService extends BaseService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (!userToAdd.contact.email) {
|
if (!userToAdd.contact.email) {
|
||||||
|
console.error("User pro email empty");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user