Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions migrations/202605101156.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Pool } from "mariadb/*";
import { MigrationParams } from "umzug";

/**
* Migration pour définir une valeur par défaut vide sur la colonne "name"
* de la table "testimony" tout en la conservant NOT NULL.
*/

export async function up({ context: pool }: MigrationParams<Pool>) {
const conn = await pool.getConnection();
try {
await conn.query(
`ALTER TABLE IF EXISTS testimony MODIFY COLUMN IF EXISTS name VARCHAR(255) NULL;`,
);
await conn.query(
`ALTER TABLE IF EXISTS testimony MODIFY COLUMN IF EXISTS created_at VARCHAR(255) NULL;`,
);
} finally {
conn.release();
}
}

export async function down({ context: pool }: MigrationParams<Pool>) {
const conn = await pool.getConnection();
try {
await conn.query(
`ALTER TABLE IF EXISTS testimony MODIFY COLUMN IF EXISTS name VARCHAR(255) NOT NULL;`,
);
await conn.query(
`ALTER TABLE IF EXISTS testimony MODIFY COLUMN IF EXISTS created_at VARCHAR(255) NOT NULL;`,
);
} finally {
conn.release();
}
}
29 changes: 29 additions & 0 deletions migrations/202605101159.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Pool } from "mariadb/*";
import { MigrationParams } from "umzug";

/**
* Migration pour définir une valeur par défaut vide sur la colonne "name"
* de la table "testimony" tout en la conservant NOT NULL.
*/

export async function up({ context: pool }: MigrationParams<Pool>) {
const conn = await pool.getConnection();
try {
await conn.query(
"ALTER TABLE IF EXISTS testimony ADD COLUMN IF NOT EXISTS `insert` BOOLEAN;",
);
} finally {
conn.release();
}
}

export async function down({ context: pool }: MigrationParams<Pool>) {
const conn = await pool.getConnection();
try {
await conn.query(
"ALTER TABLE IF EXISTS testimony DROP COLUMN IF EXISTS `insert`;",
);
} finally {
conn.release();
}
}
7 changes: 4 additions & 3 deletions src/graphql/resolvers/testimonyResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const testimonyResolver = {
_args: Record<string, never>,
context: { testimonyRepo: TestimonyRepository },
): Promise<Testimony[]> => {
return await context.testimonyRepo.getAll();
const testimonies = await context.testimonyRepo.getAll();
console.log("Retrieved testimonies:", testimonies);
return testimonies;
},

/**
Expand All @@ -43,9 +45,8 @@ const testimonyResolver = {
): Promise<boolean> => {
checkAuth(context);
const input = { ..._args.input };
if (isEmpty(input.name)) throw new Error("Name is required");
if (isEmpty(input.content)) throw new Error("Content is required");
input.name = sanitizeString(input.name);
if (input.name) input.name = sanitizeString(input.name);
if (input.company) input.company = sanitizeString(input.company);
input.content = sanitizeWysiwyg(input.content);
input.createdAt = new Date(input.createdAt || Date.now());
Expand Down
8 changes: 5 additions & 3 deletions src/graphql/schemas/testimonySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
export const testimonyTypes = `
type Testimony {
id: ID!
name: String!
name: String
company: String
content: String!
createdAt: String!
createdAt: String
insert: Boolean
}
`;
export const testimonyInputs = `
input TestimonyInput {
name: String!
name: String
company: String
content: String!
createdAt: String
insert: Boolean
}
`;

Expand Down
15 changes: 11 additions & 4 deletions src/repositories/TestimonyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class TestimonyRepository extends BaseRepository {
async getAll(): Promise<Testimony[]> {
return withConnection(this.pool, (conn) =>
conn.query(
`SELECT id, name, company, content, created_at AS createdAt FROM testimony ORDER BY created_at DESC`,
`SELECT id, name, company, content, created_at AS createdAt, \`insert\` FROM testimony ORDER BY createdAt DESC, name DESC`,
),
);
}
Expand All @@ -33,13 +33,14 @@ export default class TestimonyRepository extends BaseRepository {
const id = this.generateId();
await withConnection(this.pool, (conn) =>
conn.query(
`INSERT INTO testimony (id, name, company, content, created_at) VALUES (?, ?, ?, ?, ?)`,
`INSERT INTO testimony (id, name, company, content, created_at, \`insert\`) VALUES (?, ?, ?, ?, ?, ?)`,
[
id,
testimony.name,
testimony.company || null,
testimony.content,
testimony.createdAt || new Date().toISOString(),
testimony.insert || false,
],
),
);
Expand All @@ -56,10 +57,16 @@ export default class TestimonyRepository extends BaseRepository {
*/
async update(testimony: Partial<Testimony>): Promise<boolean> {
if (!testimony.id) throw new Error("ID is required for update");
return this.updateOne(testimony.id, {
// Utilise la clé '`insert`' pour éviter le conflit SQL
const updateData: Record<string, any> = {
name: testimony.name || undefined,
company: testimony.company || undefined,
content: testimony.content || undefined,
});
created_at: testimony.createdAt || undefined,
};
if (typeof testimony.insert !== "undefined") {
updateData["`insert`"] = testimony.insert;
}
return this.updateOne(testimony.id, updateData);
}
}
5 changes: 3 additions & 2 deletions src/types/testimonyTypes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Interface représentant un témoignage
export interface Testimony {
id: string;
name: string;
name?: string;
company?: string;
content: string;
createdAt: Date;
createdAt?: Date;
insert?: boolean;
}
Loading