0.11.20 : modify event controller for group owner
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "harmony-back",
|
"name": "harmony-back",
|
||||||
"version": "0.11.19",
|
"version": "0.11.20",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "A Strapi application",
|
"description": "A Strapi application",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -23,8 +23,10 @@ export default ({ strapi }: { strapi: Core.Strapi }) =>
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const groupId = ctx.request.body.data.group;
|
||||||
// Format tags from array of strings to array of component objects
|
// Format tags from array of strings to array of component objects
|
||||||
const data = ctx.request.body.data;
|
const data = ctx.request.body.data;
|
||||||
|
delete data.group;
|
||||||
if (data.tags && Array.isArray(data.tags)) {
|
if (data.tags && Array.isArray(data.tags)) {
|
||||||
data.tags = data.tags
|
data.tags = data.tags
|
||||||
.filter((tag: any) => {
|
.filter((tag: any) => {
|
||||||
@@ -51,14 +53,17 @@ export default ({ strapi }: { strapi: Core.Strapi }) =>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. Create the owner relationship
|
// 2. Create the owner relationship
|
||||||
|
const contextType = groupId && groupId !== 0 ? "group" : "user";
|
||||||
|
const contextId = groupId && groupId !== 0 ? groupId : userId;
|
||||||
|
|
||||||
const ownerRelationship = await strapi.db
|
const ownerRelationship = await strapi.db
|
||||||
.query("api::event-relationship.event-relationship")
|
.query("api::event-relationship.event-relationship")
|
||||||
.create({
|
.create({
|
||||||
data: {
|
data: {
|
||||||
author: userId,
|
author: userId,
|
||||||
event: event.id,
|
event: event.id,
|
||||||
contextType: "user",
|
contextType,
|
||||||
contextId: userId,
|
contextId,
|
||||||
relation: "owner",
|
relation: "owner",
|
||||||
metas: {
|
metas: {
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import type { Context } from "koa";
|
|||||||
export default ({ strapi }: { strapi: Core.Strapi }) =>
|
export default ({ strapi }: { strapi: Core.Strapi }) =>
|
||||||
async function feed(ctx: Context) {
|
async function feed(ctx: Context) {
|
||||||
const userId = ctx.state.user?.id;
|
const userId = ctx.state.user?.id;
|
||||||
|
const { contextId, contextType } = ctx.query;
|
||||||
if (!userId) return ctx.badRequest("userId is required");
|
if (!userId) return ctx.badRequest("userId is required");
|
||||||
|
|
||||||
// Récupérer et valider la query avec les fonctions de Strapi
|
// 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 }
|
{ 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
|
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
|
start = Math.max(0, parseInt(String(_start)) || 0); // Minimum 0
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -100,50 +105,44 @@ export default ({ strapi }: { strapi: Core.Strapi }) =>
|
|||||||
.map((membership) => membership.group.id)
|
.map((membership) => membership.group.id)
|
||||||
.filter((groupId) => !groupIds.includes(groupId)); // Exclure mes propres groupes
|
.filter((groupId) => !groupIds.includes(groupId)); // Exclure mes propres groupes
|
||||||
|
|
||||||
|
// 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
|
||||||
|
{ 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" },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
// 4️⃣ Récupérer le count total pour la pagination
|
// 4️⃣ Récupérer le count total pour la pagination
|
||||||
const totalCount = await strapi.db
|
const totalCount = await strapi.db
|
||||||
.query("api::event-relationship.event-relationship")
|
.query("api::event-relationship.event-relationship")
|
||||||
.count({
|
.count({
|
||||||
where: {
|
where: whereCondition,
|
||||||
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" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 4️⃣ Récupérer le feed avec pagination
|
// 4️⃣ Récupérer le feed avec pagination
|
||||||
const feed = await strapi.db
|
const feed = await strapi.db
|
||||||
.query("api::event-relationship.event-relationship")
|
.query("api::event-relationship.event-relationship")
|
||||||
.findMany({
|
.findMany({
|
||||||
where: {
|
where: whereCondition,
|
||||||
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" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
...queryParams,
|
...queryParams,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -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-11-10T13:53:36.003Z"
|
"x-generation-date": "2025-11-10T22:10:01.518Z"
|
||||||
},
|
},
|
||||||
"x-strapi-config": {
|
"x-strapi-config": {
|
||||||
"plugins": [
|
"plugins": [
|
||||||
|
|||||||
Reference in New Issue
Block a user