-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
262f42e
commit a3b0ba7
Showing
10 changed files
with
548 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const pool = require("../utils/pgpool").getPool(); | ||
|
||
const query = { | ||
editCleInsee: ` | ||
UPDATE FRONT.ADRESSE | ||
SET | ||
CLE_INSEE = $2 | ||
WHERE | ||
ID = $1 | ||
RETURNING id | ||
`, | ||
getByCleInseeOrLabel: ` | ||
SELECT | ||
ID, CLE_INSEE as "cleInsee" | ||
FROM | ||
FRONT.ADRESSE | ||
WHERE | ||
CLE_INSEE = $1 | ||
OR LABEL = $2 | ||
`, | ||
insert: ` | ||
INSERT INTO | ||
FRONT.ADRESSE ( | ||
CLE_INSEE, | ||
LABEL, | ||
CODE_INSEE, | ||
CODE_POSTAL, | ||
LONG, | ||
LAT, | ||
DEPARTEMENT | ||
) | ||
VALUES | ||
($1, $2, $3, $4, $5, $6, $7) | ||
RETURNING id | ||
`, | ||
}; | ||
|
||
const getByCleInseeOrLabel = async ({ cleInsee, label }) => { | ||
const { rows } = await pool.query(query.getByCleInseeOrLabel, [ | ||
cleInsee, | ||
label, | ||
]); | ||
return rows?.[0] ?? null; | ||
}; | ||
|
||
module.exports.saveAdresse = async (adresse) => { | ||
const existingAdresse = await getByCleInseeOrLabel({ | ||
cleInsee: adresse.cleInsee, | ||
label: adresse.label, | ||
}); | ||
|
||
if (existingAdresse && !existingAdresse.cleInsee && adresse.cleInsee) { | ||
const { rows } = await pool.query(query.editCleInsee, [ | ||
existingAdresse.id, | ||
adresse.cleInsee, | ||
]); | ||
return rows[0].id; | ||
} | ||
|
||
if (!existingAdresse) { | ||
const { rows } = await pool.query(query.insert, [ | ||
adresse.cleInsee, | ||
adresse.label, | ||
adresse.codeInsee, | ||
adresse.codePostal, | ||
adresse.coordinates[0], | ||
adresse.coordinates[1], | ||
adresse.departement, | ||
]); | ||
|
||
return rows[0].id; | ||
} | ||
|
||
return existingAdresse.id; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.