0.11.20 : modify event controller for group owner

This commit is contained in:
2025-11-10 23:10:23 +01:00
parent 539c9cd104
commit 14e013bac1
4 changed files with 44 additions and 40 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "harmony-back",
"version": "0.11.19",
"version": "0.11.20",
"private": true,
"description": "A Strapi application",
"scripts": {

View File

@@ -23,8 +23,10 @@ export default ({ strapi }: { strapi: Core.Strapi }) =>
}
try {
const groupId = ctx.request.body.data.group;
// Format tags from array of strings to array of component objects
const data = ctx.request.body.data;
delete data.group;
if (data.tags && Array.isArray(data.tags)) {
data.tags = data.tags
.filter((tag: any) => {
@@ -51,14 +53,17 @@ export default ({ strapi }: { strapi: Core.Strapi }) =>
}
// 2. Create the owner relationship
const contextType = groupId && groupId !== 0 ? "group" : "user";
const contextId = groupId && groupId !== 0 ? groupId : userId;
const ownerRelationship = await strapi.db
.query("api::event-relationship.event-relationship")
.create({
data: {
author: userId,
event: event.id,
contextType: "user",
contextId: userId,
contextType,
contextId,
relation: "owner",
metas: {
createdAt: new Date().toISOString(),

View File

@@ -4,7 +4,7 @@ import type { Context } from "koa";
export default ({ strapi }: { strapi: Core.Strapi }) =>
async function feed(ctx: Context) {
const userId = ctx.state.user?.id;
const { contextId, contextType } = ctx.query;
if (!userId) return ctx.badRequest("userId is required");
// Récupérer et valider la query avec les fonctions de Strapi
@@ -26,7 +26,12 @@ export default ({ strapi }: { strapi: Core.Strapi }) =>
{ auth: ctx.state.auth }
);
const { _limit = 20, _start = 0 } = sanitizedQueryParams;
const {
_limit = 20,
_start = 0,
contextType,
contextId,
} = sanitizedQueryParams;
limit = Math.max(1, Math.min(parseInt(String(_limit)) || 20, 100)); // Entre 1 et 100
start = Math.max(0, parseInt(String(_start)) || 0); // Minimum 0
} catch (error) {
@@ -100,11 +105,15 @@ export default ({ strapi }: { strapi: Core.Strapi }) =>
.map((membership) => membership.group.id)
.filter((groupId) => !groupIds.includes(groupId)); // Exclure mes propres groupes
// 4⃣ Récupérer le count total pour la pagination
const totalCount = await strapi.db
.query("api::event-relationship.event-relationship")
.count({
where: {
// Construire la condition where en fonction des paramètres contextId et contextType
const whereCondition =
contextId && contextType
? {
relation: "owner",
contextType,
contextId: parseInt(String(contextId)),
}
: {
relation: "owner",
$or: [
// EventRelationships de l'utilisateur courant
@@ -120,30 +129,20 @@ export default ({ strapi }: { strapi: Core.Strapi }) =>
// EventRelationships système
{ contextType: "system" },
],
},
};
// 4⃣ Récupérer le count total pour la pagination
const totalCount = await strapi.db
.query("api::event-relationship.event-relationship")
.count({
where: whereCondition,
});
// 4⃣ Récupérer le feed avec pagination
const feed = await strapi.db
.query("api::event-relationship.event-relationship")
.findMany({
where: {
relation: "owner",
$or: [
// EventRelationships de l'utilisateur courant
{ author: { id: parseInt(userId) } },
// EventRelationships des amis
{ author: { id: friendIds } },
// EventRelationships des contacts suivis
{ author: { id: followIds } },
// EventRelationships des groupes de l'utilisateur
{ contextType: "group", contextId: groupIds },
// EventRelationships des groupes des amis
{ contextType: "group", contextId: friendsGroupIds },
// EventRelationships système
{ contextType: "system" },
],
},
where: whereCondition,
...queryParams,
});

View File

@@ -14,7 +14,7 @@
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"x-generation-date": "2025-11-10T13:53:36.003Z"
"x-generation-date": "2025-11-10T22:10:01.518Z"
},
"x-strapi-config": {
"plugins": [