-
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.
- Loading branch information
1 parent
cab3285
commit fe9898b
Showing
15 changed files
with
87 additions
and
187 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 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 |
---|---|---|
@@ -1,18 +1,17 @@ | ||
import { authenticate } from "@medusajs/medusa" | ||
import { CustomerService, authenticate } from "@medusajs/medusa" | ||
import express, { Router } from "express"; | ||
import DeletedCustomerService from "../../../services/deleted-customer"; | ||
|
||
export const deleteCustomer = (router: Router) => { | ||
router.use('/admin/delete-customer', express.json()); | ||
router.use('/admin/customers/delete', express.json(), authenticate()); | ||
|
||
router.post('/admin/delete-customer', authenticate(), async (req, res) => { | ||
const deletedCustomerService = req.scope.resolve('deletedCustomerService') as DeletedCustomerService; | ||
router.post('/admin/customers/delete', async (req, res) => { | ||
try { | ||
await deletedCustomerService.deleteCustomer(req.body.email); | ||
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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]) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.