0.12.16 : add custom endpoints for contacts
All checks were successful
Build release Docker image / Build Docker Images (push) Successful in 11m26s
All checks were successful
Build release Docker image / Build Docker Images (push) Successful in 11m26s
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "harmony-back",
|
"name": "harmony-back",
|
||||||
"version": "0.12.15",
|
"version": "0.12.16",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "A Strapi application",
|
"description": "A Strapi application",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -7,6 +7,78 @@ import { factories } from "@strapi/strapi";
|
|||||||
export default factories.createCoreController(
|
export default factories.createCoreController(
|
||||||
"api::contact.contact",
|
"api::contact.contact",
|
||||||
({ strapi }) => ({
|
({ strapi }) => ({
|
||||||
|
async activities(ctx) {
|
||||||
|
const userId = ctx.state.user?.id;
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
return ctx.unauthorized("User not authenticated");
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("GET /contacts/activities - Requête reçue:", {
|
||||||
|
userId: userId,
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const friends = await strapi
|
||||||
|
.documents("api::contact.contact")
|
||||||
|
.findMany({
|
||||||
|
filters: {
|
||||||
|
owner: { id: userId },
|
||||||
|
state: "accepted",
|
||||||
|
},
|
||||||
|
populate: {
|
||||||
|
user: {
|
||||||
|
fields: ["id", "username"],
|
||||||
|
populate: {
|
||||||
|
activities: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("GET /contacts/activities - Amis trouvés:", {
|
||||||
|
count: friends.length,
|
||||||
|
});
|
||||||
|
|
||||||
|
const friendsWithLatestActivity = friends.map((contact) => {
|
||||||
|
const user = contact.user;
|
||||||
|
let latestActivity = null;
|
||||||
|
|
||||||
|
if (user.activities && Array.isArray(user.activities) && user.activities.length > 0) {
|
||||||
|
latestActivity = user.activities.reduce((latest, activity) => {
|
||||||
|
const latestDate = new Date(latest.activityDate).getTime();
|
||||||
|
const currentDate = new Date(activity.activityDate).getTime();
|
||||||
|
return currentDate > latestDate ? activity : latest;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
latestActivity: latestActivity,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: friendsWithLatestActivity,
|
||||||
|
meta: {
|
||||||
|
pagination: {
|
||||||
|
page: 1,
|
||||||
|
pageSize: friendsWithLatestActivity.length,
|
||||||
|
pageCount: 1,
|
||||||
|
total: friendsWithLatestActivity.length,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("GET /contacts/activities - Erreur:", {
|
||||||
|
error: error.message,
|
||||||
|
userId: userId,
|
||||||
|
});
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
},
|
||||||
async find(ctx) {
|
async find(ctx) {
|
||||||
// Log de la requête entrante
|
// Log de la requête entrante
|
||||||
console.log("GET /contacts - Requête reçue:", {
|
console.log("GET /contacts - Requête reçue:", {
|
||||||
@@ -72,8 +144,8 @@ export default factories.createCoreController(
|
|||||||
.documents("api::contact.contact")
|
.documents("api::contact.contact")
|
||||||
.findFirst({
|
.findFirst({
|
||||||
filters: {
|
filters: {
|
||||||
owner: owner,
|
owner: { id: owner },
|
||||||
user: user,
|
user: { id: user },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -148,10 +220,10 @@ export default factories.createCoreController(
|
|||||||
filters: {
|
filters: {
|
||||||
$or: [
|
$or: [
|
||||||
{
|
{
|
||||||
user: id,
|
user: { id: id },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
owner: id,
|
owner: { id: id },
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@@ -192,5 +264,90 @@ export default factories.createCoreController(
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async suggestions(ctx) {
|
||||||
|
const userId = ctx.state.user?.id;
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
return ctx.unauthorized("User not authenticated");
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("GET /contacts/suggestions - Requête reçue:", {
|
||||||
|
userId: userId,
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const userFriends = await strapi
|
||||||
|
.documents("api::contact.contact")
|
||||||
|
.findMany({
|
||||||
|
filters: {
|
||||||
|
owner: { id: userId },
|
||||||
|
state: "accepted",
|
||||||
|
},
|
||||||
|
populate: {
|
||||||
|
user: {
|
||||||
|
fields: ["id", "username"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("GET /contacts/suggestions - Amis trouvés:", {
|
||||||
|
count: userFriends.length,
|
||||||
|
});
|
||||||
|
|
||||||
|
const friendIds = new Set(userFriends.map((contact) => contact.user.id));
|
||||||
|
friendIds.add(userId);
|
||||||
|
|
||||||
|
const suggestedFriendIds = new Set<string>();
|
||||||
|
|
||||||
|
for (const friend of userFriends) {
|
||||||
|
const friendsFriendsContacts = await strapi
|
||||||
|
.documents("api::contact.contact")
|
||||||
|
.findMany({
|
||||||
|
filters: {
|
||||||
|
owner: { id: friend.user.id },
|
||||||
|
state: "accepted",
|
||||||
|
},
|
||||||
|
populate: {
|
||||||
|
user: {
|
||||||
|
fields: ["id", "username"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const friendFriend of friendsFriendsContacts) {
|
||||||
|
if (!friendIds.has(friendFriend.user.id)) {
|
||||||
|
suggestedFriendIds.add(JSON.stringify({
|
||||||
|
id: friendFriend.user.id,
|
||||||
|
username: friendFriend.user.username,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const suggestions = Array.from(suggestedFriendIds)
|
||||||
|
.map((str) => JSON.parse(str))
|
||||||
|
.sort((a, b) => a.username.localeCompare(b.username));
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: suggestions,
|
||||||
|
meta: {
|
||||||
|
pagination: {
|
||||||
|
page: 1,
|
||||||
|
pageSize: suggestions.length,
|
||||||
|
pageCount: 1,
|
||||||
|
total: suggestions.length,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error("GET /contacts/suggestions - Erreur:", {
|
||||||
|
error: error.message,
|
||||||
|
userId: userId,
|
||||||
|
});
|
||||||
|
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
33
src/api/contact/routes/01-custom.ts
Normal file
33
src/api/contact/routes/01-custom.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/**
|
||||||
|
* Custom contact routes
|
||||||
|
*/
|
||||||
|
|
||||||
|
export default {
|
||||||
|
type: "content-api",
|
||||||
|
routes: [
|
||||||
|
{
|
||||||
|
method: "GET",
|
||||||
|
path: "/contacts/activities",
|
||||||
|
handler: "contact.activities",
|
||||||
|
config: {
|
||||||
|
// Ajoutez ce bloc 'auth' pour être explicite
|
||||||
|
auth: {
|
||||||
|
scope: ["api::contact.contact.activities"], // Nom exact de la permission
|
||||||
|
},
|
||||||
|
policies: [], // On garde les politiques par défaut
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
method: "GET",
|
||||||
|
path: "/contacts/suggestions",
|
||||||
|
handler: "contact.suggestions",
|
||||||
|
config: {
|
||||||
|
// Ajoutez ce bloc 'auth' pour être explicite
|
||||||
|
auth: {
|
||||||
|
scope: ["api::contact.contact.activities"], // Nom exact de la permission
|
||||||
|
},
|
||||||
|
policies: [], // On garde les politiques par défaut
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
"name": "Apache 2.0",
|
"name": "Apache 2.0",
|
||||||
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
|
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
|
||||||
},
|
},
|
||||||
"x-generation-date": "2025-12-11T16:29:12.158Z"
|
"x-generation-date": "2025-12-12T18:29:01.167Z"
|
||||||
},
|
},
|
||||||
"x-strapi-config": {
|
"x-strapi-config": {
|
||||||
"plugins": [
|
"plugins": [
|
||||||
|
|||||||
Reference in New Issue
Block a user