@@ -167,6 +167,111 @@ export async function deleteCseOpinions() {
167167 }
168168}
169169
170+ /**
171+ * Insert a submitted declaration for the previous year (N-1) with job categories,
172+ * so the "Reprendre les catégories de l'année précédente" button appears.
173+ */
174+ export async function insertPreviousYearDeclaration ( ) {
175+ const sql = createConnection ( ) ;
176+ const yearResult =
177+ await sql `SELECT EXTRACT(YEAR FROM CURRENT_DATE)::int - 1 AS previous_year` ;
178+ const previousYear = yearResult [ 0 ] ?. previous_year as number ;
179+ const declId = "e2e-prev-year-decl-0000-000000000000" ;
180+
181+ try {
182+ // Get test user id
183+ const users = await sql `
184+ SELECT user_id FROM app_user_company WHERE siren = ${ TEST_SIREN } LIMIT 1
185+ ` ;
186+ const userId = users [ 0 ] ?. user_id ;
187+ if ( ! userId ) return ;
188+
189+ // Insert submitted declaration for previous year
190+ await sql `
191+ INSERT INTO app_declaration (id, siren, year, declarant_id, total_women, total_men, current_step, status, created_at, updated_at)
192+ VALUES (${ declId } , ${ TEST_SIREN } , ${ previousYear } , ${ userId } , 150, 200, 6, 'submitted', NOW(), NOW())
193+ ON CONFLICT DO NOTHING
194+ ` ;
195+
196+ // Insert 3 job categories
197+ const categories = [
198+ {
199+ id : "e2e-jobcat-1111-0000-000000000000" ,
200+ index : 0 ,
201+ name : "Cadres dirigeants" ,
202+ detail : "Directeurs et cadres supérieurs" ,
203+ } ,
204+ {
205+ id : "e2e-jobcat-2222-0000-000000000000" ,
206+ index : 1 ,
207+ name : "Ingénieurs et cadres" ,
208+ detail : "Ingénieurs, chefs de projet, managers" ,
209+ } ,
210+ {
211+ id : "e2e-jobcat-3333-0000-000000000000" ,
212+ index : 2 ,
213+ name : "Techniciens" ,
214+ detail : "Techniciens qualifiés" ,
215+ } ,
216+ ] ;
217+
218+ for ( const cat of categories ) {
219+ await sql `
220+ INSERT INTO app_job_category (id, declaration_id, category_index, name, detail, source)
221+ VALUES (${ cat . id } , ${ declId } , ${ cat . index } , ${ cat . name } , ${ cat . detail } , 'convention-collective')
222+ ON CONFLICT DO NOTHING
223+ ` ;
224+ }
225+ } finally {
226+ await sql . end ( ) ;
227+ }
228+ }
229+
230+ /** Remove the previous year test declaration and its job categories. */
231+ export async function deletePreviousYearDeclaration ( ) {
232+ const sql = createConnection ( ) ;
233+ const declId = "e2e-prev-year-decl-0000-000000000000" ;
234+
235+ try {
236+ const jobIds = [
237+ "e2e-jobcat-1111-0000-000000000000" ,
238+ "e2e-jobcat-2222-0000-000000000000" ,
239+ "e2e-jobcat-3333-0000-000000000000" ,
240+ ] ;
241+ await sql `DELETE FROM app_employee_category WHERE job_category_id = ANY(${ jobIds } )` ;
242+ await sql `DELETE FROM app_job_category WHERE declaration_id = ${ declId } ` ;
243+ await sql `DELETE FROM app_declaration WHERE id = ${ declId } ` ;
244+ } finally {
245+ await sql . end ( ) ;
246+ }
247+ }
248+
249+ /** Delete all job categories and employee categories for the test SIREN's current year declaration. */
250+ export async function deleteCurrentYearCategories ( ) {
251+ const sql = createConnection ( ) ;
252+ try {
253+ await sql `
254+ DELETE FROM app_employee_category
255+ WHERE job_category_id IN (
256+ SELECT jc.id FROM app_job_category jc
257+ INNER JOIN app_declaration d ON d.id = jc.declaration_id
258+ WHERE d.siren = ${ TEST_SIREN }
259+ AND d.year = EXTRACT(YEAR FROM CURRENT_DATE)::int
260+ )
261+ ` ;
262+ await sql `
263+ DELETE FROM app_job_category
264+ WHERE declaration_id IN (
265+ SELECT id FROM app_declaration
266+ WHERE siren = ${ TEST_SIREN }
267+ AND year = EXTRACT(YEAR FROM CURRENT_DATE)::int
268+ )
269+ ` ;
270+ } finally {
271+ await sql . end ( ) ;
272+ }
273+ }
274+
170275type CampaignDeadlineDates = {
171276 decl1ModificationDeadline : string ;
172277 decl1JustificationDeadline : string ;
0 commit comments