-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from curiosta/feat/add-user-deletion
Feat/add user deletion
- Loading branch information
Showing
11 changed files
with
133 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const { DataSource } = require("typeorm"); | ||
const dotenv = require("dotenv"); | ||
|
||
dotenv.config() | ||
|
||
const AppDataSource = new DataSource({ | ||
type: "postgres", | ||
port: 5432, | ||
port: process.env.DB_PORT, | ||
username: process.env.DB_USER, | ||
password: process.env.DB_PASSWORD, | ||
database: process.env.DB_NAME, | ||
host: process.env.DB_HOST, | ||
entities: [ | ||
], | ||
migrations: [ | ||
"dist/migrations/*.js", | ||
], | ||
}) | ||
|
||
module.exports = { | ||
datasource: AppDataSource, | ||
} |
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,17 @@ | ||
import { CustomerService, authenticate } from "@medusajs/medusa" | ||
import express, { Router } from "express"; | ||
|
||
export const deleteCustomer = (router: Router) => { | ||
router.use('/admin/customers/delete', express.json(), authenticate()); | ||
|
||
router.post('/admin/customers/delete', async (req, res) => { | ||
try { | ||
const customerService = req.scope.resolve('customerService') as CustomerService; | ||
const customer = await customerService.retrieveRegisteredByEmail(req.body.email) | ||
await customerService.delete(customer.id) | ||
return res.json({ status: 200, message: 'User has been deleted!' }) | ||
} catch (error) { | ||
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error }) | ||
} | ||
}) | ||
} |
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,17 @@ | ||
import { authenticate } from "@medusajs/medusa" | ||
import { Router } from "express"; | ||
import CustomerService from "../../../services/customer"; | ||
|
||
export const listDeletedCustomers = (router: Router) => { | ||
router.use('/admin/customers/list-deleted', authenticate()); | ||
|
||
router.get('/admin/customers/list-deleted', async (req, res) => { | ||
const customerService = req.scope.resolve('customerService') as CustomerService | ||
try { | ||
const result = await customerService.retrieveDeletedCustomer() | ||
return res.json(result) | ||
} catch (error) { | ||
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error }) | ||
} | ||
}) | ||
} |
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,21 @@ | ||
import { authenticate } from "@medusajs/medusa" | ||
import express, { Router } from "express"; | ||
import CustomerService from "../../../services/customer"; | ||
|
||
export const restoreCustomer = (router: Router) => { | ||
router.use('/admin/customers/restore', express.json(), authenticate()); | ||
|
||
router.post('/admin/customers/restore', async (req, res) => { | ||
|
||
if (!req.body.email) return res.status(422).json({ status: 422, message: 'Insufficient payload!' }) | ||
|
||
try { | ||
const customerService = req.scope.resolve('customerService') as CustomerService | ||
await customerService.restoreCustomer(req.body.email); | ||
|
||
return res.json({ status: 200, message: 'User has been restored!' }) | ||
} catch (error) { | ||
return res.status(500).json({ status: 500, message: 'An error occurred!', error: error instanceof Error ? error.message : error }) | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { | ||
dataSource, | ||
} from "@medusajs/medusa/dist/loaders/database" | ||
import { DeletedCustomer } from "../models/deleted-customer" | ||
|
||
export const DeletedCustomerRepository = dataSource.getRepository(DeletedCustomer) | ||
export default DeletedCustomerRepository |
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,39 @@ | ||
import { CustomerService as BaseCustomerService, Customer } from '@medusajs/medusa' | ||
|
||
export default class CustomerService extends BaseCustomerService { | ||
constructor(container) { | ||
super(container) | ||
} | ||
|
||
|
||
async retrieveDeletedCustomer() { | ||
const deletedCustomerQuery = ` | ||
SELECT id, email, first_name, last_name, billing_address_id, phone, has_account, metadata, created_at, updated_at, deleted_at FROM customer WHERE deleted_at IS NOT NULL | ||
` | ||
|
||
const listOfDeletedCustomers = await this.activeManager_.query(deletedCustomerQuery) | ||
|
||
return listOfDeletedCustomers | ||
} | ||
|
||
async restoreCustomer(email: string) { | ||
// check if email exists! | ||
|
||
const customerByEmailQuery = ` | ||
SELECT * FROM customer WHERE email = $1 | ||
` | ||
|
||
const customerResult = await this.activeManager_.query(customerByEmailQuery, [email]) as Customer[] | ||
|
||
if (!customerResult.length) { | ||
console.log(customerResult); | ||
throw new Error('Customer not found with this email!') | ||
} | ||
|
||
const restoreCustomerQuery = ` | ||
UPDATE customer SET deleted_at = NULL WHERE email = $1 | ||
` | ||
|
||
await this.activeManager_.query(restoreCustomerQuery, [email]) | ||
} | ||
} |